wait.c
pennmush-1.8.3p3/src/wait.c · 2.8 KB
From the archive pennmush-1.8.3p3.tar.gz
Download the archive « Back to the archive contents
/**
* \file wait.c
*
* \brief Process and process-group control functions.
*/
#include "config.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef I_SYS_TYPES
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#include <errno.h>
#include "wait.h"
/** Portable wait
* \param child pid of specific child proccess to wait for. Only meaningful if HAVE_WAITPID is defined.
* \param status pointer to store the child process's exit status in.
* \param flags optional flags to pass to waitpid() or wait3().
* \return pid of child process that exited, or -1.
*/
pid_t
mush_wait(pid_t child __attribute__ ((__unused__)),
WAIT_TYPE * status __attribute__ ((__unused__)),
int flags __attribute__ ((__unused__)))
{
#if defined(HAVE_WAITPID)
return waitpid(child, status, flags);
#elif defined(HAVE_WAIT3)
return wait3(status, flags, NULL);
#elif defined(HAVE_WAIT)
/* Wait as long as it's okay to block */
if (flags == 0)
return wait(status);
else
return -1;
#else
/* Not implemented */
return -1;
#endif
}
/** Portable setpgid()/setpgrp()
* \param pid process to change group. 0 for current process.
* \param pgrp new process group.
* \return 0 on success, -1 on failure.
*/
int
set_process_group(pid_t pid __attribute__ ((__unused__)),
pid_t pgrp __attribute__ ((__unused__)))
{
#if defined(HAVE_SETPGID)
return setpgid(pid, pgrp);
#elif defined(HAVE_SETPGRP)
#ifndef VOID_SETPGRP
return setpgrp(pid, pgrp);
#else
return setpgrp();
#endif
#else
return 0;
#endif
}
/** Create a new process group if possible.
* Equivalent to set_process_group(0, getpid()).
* \return 0 on success, -1 on failure
*/
int
new_process_group(void)
{
return set_process_group(0, getpid());
}
/** Create a new process session if possible.
* See documentation for setsid(2). If that system call
* is not available, make a new process group.
* \return 0 on success, -1 on failure
*/
int
new_process_session(void)
{
#if defined(HAVE_SETSID)
return setsid();
#else
/* Close enough for government work */
return new_process_group();
#endif
}
/**
* Lowers the scheduling priority of a process by a given amount. Note that
* on Unix, the higher the number, the lower the priority.
* \param pid the process id of the process to change.
* \param prio how much to add to the priority.
* \return 0 on success, -1 on error.
*/
int
lower_priority_by(pid_t pid, int prio)
{
int newprio = 0;
#ifdef HAVE_GETPRIORITY
errno = 0;
if ((newprio = getpriority(PRIO_PROCESS, pid)) < 0) {
if (errno != 0)
return -1;
}
#endif
newprio += prio;
if (newprio > 20)
newprio = 20;
#ifdef HAVE_SET_PRIORITY
return setpriority(PRIO_PROCESS, pid, newprio);
#else
return 0;
#endif
}