whats safe after a vfork?

whats safe after a vfork?

Post by Albert Lun » Wed, 29 Jan 1997 04:00:00



In reading about vfork, it is suggested that a vfork be followed
immediately with an exec. I'm trying to understand the possible
implications of a program that does a bunch of I/O redirection
in the child before the exec.

Here's a code fragment I wonder about:
= =

    iop = NULL;
    switch (pid = vfork()) {
    case -1:                    /* error */
        (void) close(pdes[0]);
        (void) close(pdes[1]);
        goto pfree;
        /* NOTREACHED */
    case 0:                 /* child */
        if (*type == 'r') {
            if (pdes[1] != 1) {
                dup2(pdes[1], 1);
                if (closestderr)
                    (void) close(2);
                else
                    dup2(pdes[1], 2);  /* stderr, too! */
                (void) close(pdes[1]);
            }
            (void) close(pdes[0]);
        } else {
            if (pdes[0] != 0) {
                dup2(pdes[0], 0);
                (void) close(pdes[0]);
            }
            (void) close(pdes[1]);
        }
        for (i = 3; i < fds; i++)
                close(i);
        /* begin CERT suggested fixes */
        close(0);
        i = geteuid();
        delay_signaling(); /* we can't allow any signals while euid==0: kinch */
        seteuid(0);
        setgid(getegid());
        setuid(i);
        enable_signaling(); /* we can allow signals once again: kinch */
        /* end CERT suggested fixes */
        execv(gargv[0], gargv);
        _exit(1);
    }
    /* parent; assume fdopen can't fail...  */
    if (*type == 'r') {
        iop = fdopen(pdes[0], type);
        (void) close(pdes[1]);
    } else {
        iop = fdopen(pdes[1], type);
        (void) close(pdes[0]);
    }
    pids[fileno(iop)] = pid;

= =

(This is from the wuftpd version wu-2.4.2-academ[BETA-12] in the ftpd_popen
 routine in popen.c, for those who care about context.)

I'm concerned that all that messing about in the child will affect
the memory space of the parent too, since this is a vfork, but
I'm not sure exactly  what is shared across a vfork.

Stuff using this code has bus errors and segmentation faults more
that I'd like so I'm looking for potentital memory clobbers.

--

 
 
 

whats safe after a vfork?

Post by Roger Espel Lli » Thu, 30 Jan 1997 04:00:00



| In reading about vfork, it is suggested that a vfork be followed
| immediately with an exec. I'm trying to understand the possible
| implications of a program that does a bunch of I/O redirection
| in the child before the exec.
|
| Here's a code fragment I wonder about:

 [ ... ]
|
| (This is from the wuftpd version wu-2.4.2-academ[BETA-12] in the ftpd_popen
|  routine in popen.c, for those who care about context.)
|
| I'm concerned that all that messing about in the child will affect
| the memory space of the parent too, since this is a vfork, but
| I'm not sure exactly  what is shared across a vfork.
|
| Stuff using this code has bus errors and segmentation faults more
| that I'd like so I'm looking for potentital memory clobbers.

Pretty much any libc call might be implemented in a way that changes the
state of the library, making it unsuitable between a vfork() and an
exec().  Under Solaris this is the case with signal(), which is one
thing you'd commonly want to call between [v]fork() and exec().  In
doubt, use fork().

        Roger

 
 
 

whats safe after a vfork?

Post by Shado » Thu, 30 Jan 1997 04:00:00



Quote:>In reading about vfork, it is suggested that a vfork be followed
>immediately with an exec. I'm trying to understand the possible
>implications of a program that does a bunch of I/O redirection
>in the child before the exec.

The manual pages on my Solaris warn about using _exit() if you can't
exec, since exit() would close the descriptors and flush them.vfork will
run the in the parents address space unless you exec which causes it to
run somewhere else.

I can't think of using vfork unless I wanted to execute lots of other
proccesses, if it came to that, I'd fork, then change I/O and vfork the X
many proccesses this would be more effecient.....

------------------------------------------------------------------------------
Thamer Al-Herbish (ShadowS)     The views expressed here, have no relevance


                -=whitefang dawt kawm=-
-------------------------------------------------------------------------------

 
 
 

whats safe after a vfork?

Post by James Youngm » Fri, 31 Jan 1997 04:00:00



Quote:

>In reading about vfork, it is suggested that a vfork be followed
>immediately with an exec. I'm trying to understand the possible
>implications of a program that does a bunch of I/O redirection
>in the child before the exec.

IMHO it's not worth the worry since most UNIX implementations implement vfork()
with fork() since they have COW pages -- fork() just doesn't have the overhead
it used to.

--
James Youngman       VG Gas Analysis Systems |The trouble with the rat-race
 Before sending advertising material, read   |is, even if you win, you're
http://www.law.cornell.edu/uscode/47/227.html|still a rat.

 
 
 

whats safe after a vfork?

Post by Boyd Rober » Fri, 31 Jan 1997 04:00:00



Quote:

>IMHO it's not worth the worry since most UNIX implementations implement vfork()
>with fork() since they have COW pages -- fork() just doesn't have the overhead
>it used to.

vfork() always was, and always will be, a crock.  stick to fork().

--

``Not only is UNIX dead, but it's starting to smell really bad.''  -- rob

 
 
 

whats safe after a vfork?

Post by Michael F Gord » Thu, 06 Feb 1997 04:00:00



Quote:>vfork() always was, and always will be, a crock.  stick to fork().

Agreed, the semantics of vfork() are a little weird, but it does have
some uses.  If you have a very large program which is going to fork()
and exec() something much smaller, vfork() can be a win, although now
that almost every version of Unix does COW this isn't as important as
it was.  vfork() does still allow you to get around the problem of
what to do if you haven't got the swap space for two copies of the
large program - do you make fork() fail or do you fork() anyway and do
lazy swap allocation and possibly have to kill the process at a random
time?  vfork() means that you only need enough swap space for one
copy of the large program and one of the small program.

Michael Gordon
--
Quidquid latine dictum sit, altum viditur.

 
 
 

whats safe after a vfork?

Post by Andrew E. Miles » Thu, 06 Feb 1997 04:00:00




: >vfork() always was, and always will be, a crock.  stick to fork().
:
: Agreed, the semantics of vfork() are a little weird, but it does have
: some uses.

Last time I checked, in Linux vfork() is implemented as fork().

--

Linux Plug-and-Play Kernel Project http://www.redhat.com/linux-info/pnp/
XFree86 Matrox Team http://www.bf.rmit.edu.au/~ajv/xf86-matrox.html

 
 
 

1. Informix problems afters Y2K Solaris 2.5 patch

Since I did the Y2K patch for Solaris 2.5 on a Ultra Sparc 1 system,
Informix 7.22 refuses to startup properly.
Anyone experienced a simular problem? Any suggestions?
Attached some system information and the relevant part of the online.log

Has van der Krieken.

# uname -a
SunOS sungemms 5.5 Generic_103093-25 sun4u sparc SUNW,Ultra-1
# su - informix
Sun Microsystems Inc.   SunOS 5.5       Generic November 1995
$ onstat -V
INFORMIX-OnLine Version 7.22.UC2   Software Serial Number AAA#J917741
$ cat online.log

Mon Dec 27 16:09:48 1999

16:09:48  Event alarms enabled.  ALARMPROG = '/informix/etc/no_log.sh'
16:09:48  DR: DRAUTO is 0 (Off)
16:09:49  INFORMIX-OnLine Initialized -- Shared Memory Initialized.
16:09:49  Physical Recovery Started.
16:09:49  Physical Recovery Complete: 0 Pages Restored.
16:09:49  Logical Recovery Started.
16:09:49  shmget: [ENOMEM][12]: key 52574804: out of shared memory, check
system
 SHMALL
16:09:49  out of virtual shared memory

16:09:49  Assert Failed: Internal Error - Segmentation Violation

                Thread(13, bld_logrecs, d039530, 1)
16:09:49   Results: OnLine must abort
16:09:49   Action: Reinitialize shared memory
16:09:49   See Also: /var/tmp/af.d813d
16:09:49  rsinit.c, line 9932, thread 13, proc id 20737, Segmentation
Violation.
16:09:49    PANIC: Attempting to bring system down

2. Help! IPC panics when I try and connect using cu or ppp

3. callback, ppp, and morning-afters

4. Running Afterstep on the secon Visual ?

5. Solaris 2.6: 'Async safe' implies 'thread safe' ?

6. Spurious Interrupt Message - what does it mean?

7. Solaris 8 threads: If a routine is Async-Signal-Safe is it also thread Safe?

8. Stoopid Knews Question

9. mmap, thread-safe, and signal-safe

10. Thread-safe and Signal-safe

11. Difference Between 'Safe' And 'MT-Safe' ML-Level

12. Why does free() do a vfork() !?

13. Making a popen() replacement with vfork()