home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
kermit.columbia.edu
/
kermit.columbia.edu.tar
/
kermit.columbia.edu
/
sredird
/
telnetcpcd-1.09.tar.gz
/
telnetcpcd-1.09.tar
/
wait_child.c
< prev
Wrap
C/C++ Source or Header
|
2003-08-12
|
2KB
|
71 lines
/*
wait_child.c
Copyright (c) 2002,2003 Thomas J Pinkl <tom@pinkl.com>
reap the exit status of a child process
Version 1.00 12/03/2001
*/
#include "telnetcpcd.h"
pid_t wait_child(pid_t pid,int *statloc)
{
extern int errno;
pid_t ret;
int *status;
int wstatus;
if (statloc != (int *) NULL) {
status = statloc;
} else {
status = &wstatus;
}
ret = waitpid(pid,status,WNOHANG);
if ((ret == (pid_t) 0) && (pid != (pid_t) -1)) {
/*
pid == -1 means caller is polling. don't retry in this case.
status not yet available for specified pid.
we'll wait a second and try again
*/
syslog(LOG_DEBUG,"waitpid() returned 0; will try again");
sleep(1);
ret = waitpid(pid,status,WNOHANG);
}
if (ret == (pid_t) -1) {
if (errno == ECHILD) {
;
/* syslog(LOG_DEBUG,"waitpid(): no more child processes"); */
} else {
syslog_perror("waitpid() error");
}
} else {
if (ret > 0) {
log_termination_status(ret,*status);
}
}
return(ret);
}
/*
interpret a child's exit status and send the
result to the syslog
*/
void log_termination_status(pid_t pid,int status)
{
char *str;
str = "child PID";
if (WIFEXITED(status)) {
syslog(LOG_ERR,"%s %d terminated normally by calling exit(%d)",
str,pid,WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
syslog(LOG_ERR,"%s %d terminated by %s signal (number %d)",
str,pid,signame(WTERMSIG(status)),WTERMSIG(status));
} else {
syslog(LOG_ERR,"%s %d unknown termination status, value: %d",
str,pid,status);
}
}