zsh-workers
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@candle.brasslantern.com>
To: zsh-workers@sunsite.auc.dk
Subject: PATCH (?): Re: wait for non-child PID
Date: Thu, 27 Jul 2000 06:57:08 +0000	[thread overview]
Message-ID: <1000727065708.ZM26483@candle.brasslantern.com> (raw)
In-Reply-To: <hvoaef56z0x.fsf@serein.m17n.org>
In-Reply-To: <20000726190953.A22895@scowler.net>
In-Reply-To: <hvolmyoqhol.fsf@serein.m17n.org>

On Jul 26,  6:06pm, Tanaka Akira wrote:
}
} I heard that wait has a problem if the argument is non-child PID.
} [Zsh] blocks forever.  (It is interruptible.)
} 
} bash detects that the PID is not child of the shell.

Hmm, this is an interesting portability problem.  Zsh does sometimes
create child processes that it might conceivably be reasonable for a
script to wait for, but that are not listed in its job tables.  So
there's no obvious internal way to check whether a PID is a child, and
we can't rely on waitpid() or wait4() being available.

A quick check with strace (without looking at source) shows that bash
reports "is not a child" without using any system calls, so it must be
using a table of child process IDs.

On Jul 26,  7:09pm, Clint Adams wrote:
}
} I'm glad that zsh's wait will wait on processes that aren't children of the
} shell.  Is there a reason that it shouldn't?

One obvious counter-example is `wait $$' ...

The `wait' builtin uses zsh's waitforpid() function, which loops doing
kill(pid,0) to make sure the process is still running, and if so rather
than use wait4() or the like (see above) it does a sigpause() or the
equivalent.  The kill will eventually fail for any process that exits;
but zsh only comes out of the sigpause() when it gets a signal of some
kind, which may never happen if the waited-for pid is not a child.

On Jul 27,  2:13pm, Tanaka Akira wrote:
} 
} A zsh can waits the process, but the zsh doesn't notice its status
} change because the zsh is not the parent of it and SIGCHLD cannot
} reached to the zsh.  Is it useful?

It's certainly not useful as currently implemented.  It'd at least have
to recognize that waiting when (pid == 1 || pid == getpid()) is silly,
and it'd have to use a polling time-out for any PID that is not known
to be a child.

Short of doing that, the only thing to do seems to be to rely on the job
table.  I won't commit the following patch until we're reasonably sure
that there are no interesting cases of child processes that can't be
detected by findproc() -- something I'm not entirely certain of myself.

Also, we might want to change the error message to be ksh-like rather
than bash-like, if there's a difference.

Note in passing:  bash prints "bash: wait: ..." but zwarnnam() prints
only "wait: ..." and zwarn() prints only "zsh: ...".  Oh, well.

Index: Src/jobs.c
===================================================================
@@ -1289,7 +1289,13 @@
 
 	if (func == BIN_WAIT && isanum(*argv)) {
 	    /* wait can take a pid; the others can't. */
-	    waitforpid((long)atoi(*argv));
+	    pid_t pid = (long)atoi(*argv);
+	    Job j;
+	    Process p;
+	    if (findproc(pid, &j, &p))
+		waitforpid(pid);
+	    else
+		zwarnnam(name, "pid %d is not a child of this shell", 0, pid);
 	    retval = lastval2;
 	    thisjob = ocj;
 	    continue;

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


  reply	other threads:[~2000-07-27  6:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-07-26  9:06 Tanaka Akira
2000-07-26 23:09 ` Clint Adams
2000-07-27  5:13   ` Tanaka Akira
2000-07-27  6:57     ` Bart Schaefer [this message]
2000-07-27 17:22       ` PATCH (?): " Bart Schaefer
2000-07-28  7:04 Sven Wischnowsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1000727065708.ZM26483@candle.brasslantern.com \
    --to=schaefer@candle.brasslantern.com \
    --cc=zsh-workers@sunsite.auc.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).