Plan 9 from Bell Labs’s /usr/web/sources/patch/applied/apeldr03/wait.c.new

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include "lib.h"
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include "sys9.h"

<<<<<<< wait.c.orig
=======
/*
**	PID cache
*/
typedef struct wdesc wdesc;
struct wdesc {
	pid_t w_pid;
	Waitmsg *w_msg;
	wdesc *w_next;
};
static wdesc *wd = 0;

static Waitmsg *
lookpid (pid_t pid) {
	wdesc **wp0 = &wd, *wp;
	Waitmsg *msg;

	if (pid == -1) {
		if (wd == 0)
			return 0;
		pid = wd->w_pid;
	}
	for (wp = wd; wp; wp = wp->w_next) {
		if (wp->w_pid == pid) {
			msg = wp->w_msg;
			*wp0 = wp->w_next;
			free (wp);
			return msg;
		}
		wp0 = &(wp->w_next);
	}
	return 0;
}

static void
addpid (Waitmsg *msg) {
	wdesc *wp = malloc (sizeof (wdesc));

	wp->w_msg = msg;
	wp->w_pid = msg->pid;
	wp->w_next = wd;
	wd = wp;
}

pid_t
wait (int *status) {
	return wait4(-1, status, 0, 0);
}

>>>>>>> wait.c
pid_t
<<<<<<< wait.c.orig
wait(int *stat_loc)
{
	return waitpid(-1, stat_loc, 0);
=======
waitpid (pid_t wpid, int *status, int options) {
	return wait4(wpid, status, options, 0);
>>>>>>> wait.c
}

pid_t
<<<<<<< wait.c.orig
waitpid(int pid, int *stat_loc, int options)
{
	int n, i, wfd, r, t, wpid;
	char *bp, *ep, pname[50];
	struct stat buf;
=======
wait3 (int *status, int options, Waitmsg *waitmsg) {
	return wait4(-1, status, options, waitmsg);
}

pid_t
wait4 (pid_t wpid, int *status, int options, Waitmsg *waitmsg) {
>>>>>>> wait.c
	Waitmsg *w;

<<<<<<< wait.c.orig
	if(options&WNOHANG){
		sprintf(pname, "/proc/%d/wait", getpid());
		i = stat(pname, &buf);
		if(i >=0 && buf.st_size==0)
=======
	if (options & WNOHANG) {
		char pname[128];
		int i;
		struct stat buf;

		snprintf (pname, sizeof (pname), "/proc/%d/wait", getpid());
		i = stat (pname, &buf);
		if (i >= 0 && buf.st_size == 0)
>>>>>>> wait.c
			return 0;
	}
<<<<<<< wait.c.orig
	n = 0;
	while(n==0){
		w = _WAIT();
		if(w == 0){
			_syserrno();
			n = -1;
		}else{
=======
	if (w = lookpid (wpid)) {
		waitmsg = w;
		wpid = w->pid;
		return wpid;
	}
	w = _WAIT();
	while (w) {
		if (wpid <= 0) {
			waitmsg = w;
>>>>>>> wait.c
			wpid = w->pid;
<<<<<<< wait.c.orig
			if(pid>0 && wpid!=pid){
				free(w);
				continue;
			}
			n = wpid;
			if(stat_loc){
				r = 0;
				t = 0;
				if(w->msg[0]){
=======
			return wpid;
		}
		if (w->pid == wpid) {
			if (status) {
				int r = 0;
				int t = 0;
				char *bp, *ep;

				if (w->msg[0]) {
>>>>>>> wait.c
					/* message is 'prog pid:string' */
					bp = w->msg;
<<<<<<< wait.c.orig
					while(*bp){
						if(*bp++ == ':')
=======
					while (*bp) {
						if (*bp++ == ':')
>>>>>>> wait.c
							break;
					}
<<<<<<< wait.c.orig
					if(*bp == 0)
=======
					if (*bp == 0)
>>>>>>> wait.c
						bp = w->msg;
<<<<<<< wait.c.orig
					r = strtol(bp, &ep, 10);
					if(*ep == 0){
						if(r < 0 || r >= 256)
=======
					r = strtol (bp, &ep, 10);
					if (*ep == 0) {
						if (r < 0 || r >= 256)
>>>>>>> wait.c
							r = 1;
<<<<<<< wait.c.orig
					}else{
						t = _stringsig(bp);
						if(t == 0)
=======
					} else {
						t = _stringsig (bp);
						if (t == 0)
>>>>>>> wait.c
							r = 1;
					}
				}
<<<<<<< wait.c.orig
				*stat_loc = (r << 8) | t;
=======
				*status = (r << 8) | t;
>>>>>>> wait.c
			}
<<<<<<< wait.c.orig
			free(w);
=======
			waitmsg = w;
			wpid = w->pid;
			return wpid;
		} else {
			addpid (w);
>>>>>>> wait.c
		}
<<<<<<< wait.c.orig
=======
		w = _WAIT();
	}
	if (w == 0) {
		_syserrno ();
>>>>>>> wait.c
	}
<<<<<<< wait.c.orig
	return n;
=======
>>>>>>> wait.c
}
<<<<<<< wait.c.orig

=======
>>>>>>> wait.c

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.