Write a new static system call to incorporate to your custom Linux Kernel. The s
ID: 3902691 • Letter: W
Question
Write a new static system call to incorporate to your custom Linux Kernel. The system call you write should take one argument and return the process state information for that process, its child processes, child threads and sibling processes. The prototype for your system call will be:
asmlinkage long sys_pinfo(struct pinfo *info);
pid_t is defined in /usr/include/sys/types.h and /usr/include/linux/types.h for user-space programs and in /usr/src/linux/include/linux/types.h (/usr/src/linux/include/uapi/asm-generic/posix_types.h) for kernel-space routines.
Also see /usr/include/asm-generic/posix_types.h
Define struct pinfo in /usr/src/linux/include/linux/pinfo.h as part of your solution (localize it for your test C program):
#include <linux/types.h>
struct pinfo {
pid_t pid; /* process id */
long state; /* current state of process */
long nice; /* process nice value */
pid_t parent_pid; /* process id of parent */
int nr_children; /* total number of child processes */
int nr_threads; /* total number of child threads */
pid_t youngest_child_pid; /* pid of youngest child */
pid_t younger_sibling_pid; /* pid of younger sibling */
pid_t older_sibling_pid; /* pid of older sibling */
unsigned long start_time; /* process start time */
long user_time; /* CPU time spent in user mode */
long sys_time; /* CPU time spent in system mode */
long cutime; /* total user time of children */
long cstime; /* total system time of children */
long uid; /* user id of process owner */
char comm[16]; /* name of program executed */
};
Sibling processes are those sharing the same parent. Younger, youngest and older comparisons between processes are made based on their start time.
Your system call should return 0 unless an error occurs. Your code should handle errors that can occur but not handle any errors that cannot occur. At a minimum, your system call should detect the following conditions and respond as described:
If the address for the pinfo structure is null, return -22.
If a value to be set in pinfo is accessible through a pointer which is null, set the corresponding field value in pinfo to -1. For example, the youngest_child_pid should be set to -1 if the process does not have a child. The referenced error code is defined as EINVAL in /usr/src/linux/include/uapi/asm-generic/errno-base.h
/usr/src/linux/include/linux/errno.h
/usr/src/linux/usr/include/asm/errno.h.
Each system call must be assigned a number which becomes its position in the system call vector. You must identify an available spot for your new system call in the vector for x86_64 system calls (see /usr/src/linux/arch/x86/syscalls/syscall_64.tbl and /usr/src/linux/include/uapi/asm-generic/unistd.h)
Linux maintains a list of all processes in a doubly linked list. Each entry in this list is a task_struct structure, which is defined in /usr/src/linux/include/linux/sched.h.
In /usr/src/linux/include/asm-generic/current.h, “current” is defined to inline a function which returns the address of the task_struct of the currently running process. All of the information to be returned in the pinfo structure can be determined by starting with “current”. Note that current.h includes /usr/src/linux/include/linux/thread_info.h
See /usr/src/linux/arch/x86/include/asm/thread_info.h
See code in:
/usr/src/linux/arch/x86/syscalls/syscall_64.tbl
/usr/src/linux/arch/x86/syscalls/syscall_32.tbl
Also see:
/usr/src/linux/include/uapi/asm-generic/unistd.h
/usr/include/asm/unistd.h
/usr/src/linux/include/linux/syscalls.h
/usr/src/linux/kernel/kallsyms.c
Add the following line below the entry that starts with 326 to /usr/src/linux/arch/x86/syscalls/syscall_64.tbl
327 common mypinfo sys_pinfo
Add the following lines to /usr/src/linux/include/linux/syscalls.h at the proper spots:
struct pinfo;
…
#include <linux/pinfo.h>
…
asmlinkage long sys_pinfo(struct pinfo *info);
Create a directory in /usr/src/linux and call it pinfo. This directory should contain the implementation of your new static system call.
Edit the main Makefile in /usr/src/linux/Makefile and add the new pinfo/ directory to the following line:
core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ pinfo/
Create a Makefile in /usr/src/linux/pinfo with the following line in it:
obj-y := pinfo.o
Develop and implement the code of your new static system call in /usr/src/linux/pinfo/pinfo.c The following should serve as a base template. Make sure to author and properly document your code in this source file.
See /usr/src/linux/include/linux/cred.h
See /usr/src/linux/include/linux/uidgid.h
struct pinfo;
#include <linux/syscalls.h>
#include <linux/pinfo.h>
#include <linux/linkage.h>
#include <asm/current.h>
#include <linux/list.h>
#include <linux/time.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/sched.h>
…
asmlinkage long sys_pinfo(struct pinfo *info)
{
// Local Declarations
…
if(info == NULL)
return -22;
// Main Logic
…
return 0;
}
Build your custom kernel and re-deploy it following the same steps you performed in Assignment 1 when you initially built and deployed the custom kernel in your system. Then make sure to boot your system from the custom made kernel that now includes the new system call and proceed to run your test program to verify the correctness of your new system call routine.
Testing your System Call
To test your system call, write a simple program which creates a few child processes and a few child threads before it calls the new pinfo system call.Your program should print all the process state information for the calling process. Run the program several times. Which fields in the pinfo structure change? Which ones do not? For each field in the pinfo structure, discuss how frequently it changes and why as part of your experiments and document in your learning report.
Although system calls are generally accessed through a library (glibc), your test program should access your system call directly. This is accomplished by utilizing the syscall() function. See man syscall. Your test program may include code like the following:
#include <unistd.h>
#include <stdio.h>
#include “pinfo.h”
Int main()
{
struct pinfo p_info;
int status;
… /* create child processes and keep ‘em active */
… /* create child threads and keep’em active */
/* If this is the parent process, call new system call… */
status = syscall (327, &p_info); //NOTE: use proper system call number designated in the system call table file (/usr/src/linux/arch/x86/syscalls/syscall_64.tbl)
/* … and output to standard output the info returned by new system call */
printf("p_info: ");
printf(
"pid = %d "
"state = %ld "
"nice = %ld "
"parent_pid = %d "
"children = %d "
"youngest_child_pid = %d "
"younger_sibling_pid = %d "
"older_sibling_pid = %d "
"start_time = %lu "
"user_time = %ld "
"sys_time = %ld "
"uid = %ld "
"comm = %s ",
p_info.pid, p_info.state, p_info.nice, p_info.parent_pid, p_info.children,
p_info.youngest_child_pid, p_info.younger_sibling_pid, p_info.older_sibling_pid,
p_info.start_time, p_info.user_time, p_info.sys_time, p_info.uid, p_info.comm);
exit(0);
}
Consider setting up a shell script from which you start several background processes (i.e. sleep x) prior to launching your testing C program so as to register siblings with a common parent. The child processes and child threads created from within your testing C program should block or sleep long enough to allow the new system call to collect statistics for their parent.
The output of the program should be easy to read. The ps and top commands will provide valuable help in verifying the accuracy of information printed by your program. You can access detailed information on these commands on the man pages.
Explanation / Answer
#ifdef LINUX
# include <asm/unistd.h>
# if defined __NR_tgkill
# define my_tgkill(pid, tid, sig) syscall (__NR_tgkill, (pid), (tid), (sig))
# elif defined __NR_tkill
# define my_tgkill(pid, tid, sig) syscall (__NR_tkill, (tid), (sig))
# else
/* kill() may choose arbitrarily the target task of the process group
while we later wait on a that specific TID. PID process waits become
TID task specific waits for a process under ptrace(2). */
# warning "Neither tkill(2) nor tgkill(2) available, risk of strace hangs!"
# define my_tgkill(pid, tid, sig) kill ((tid), (sig))
# endif
#endif
#if defined(IA64) && defined(LINUX)
# include <asm/ptrace_offsets.h>
#endif
#ifdef USE_PROCFS
#include <poll.h>
#endif
#ifdef SVR4
#include <sys/stropts.h>
#ifdef HAVE_MP_PROCFS
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#endif
#endif
extern char **environ;
extern int optind;
extern char *optarg;
// pgbovine
#include "okapi.h"
extern char CDE_exec_mode;
extern char CDE_verbose_mode; // -v option
extern char CDE_exec_streaming_mode; // -s option
extern char CDE_block_net_access; // -n option
extern char CDE_use_linker_from_package; // ON by default, -l option to turn OFF
extern void alloc_tcb_CDE_fields(struct tcb* tcp);
extern void free_tcb_CDE_fields(struct tcb* tcp);
extern void strcpy_redirected_cderoot(char* dst, char* src);
extern void CDE_init_tcb_dir_fields(struct tcb* tcp);
extern FILE* CDE_copied_files_logfile;
extern char* CDE_PACKAGE_DIR;
extern void CDE_clear_options_arrays();
extern void CDE_add_ignore_exact_path(char* p);
extern void CDE_add_ignore_prefix_path(char* p);
int debug = 0, followfork = 1; // pgbovine - turn on followfork by default, can de-activate using the '-f' option
unsigned int ptrace_setoptions = 0;
int dtime = 0, xflag = 0, qflag = 1; // pgbovine - turn on quiet mode (-q) by
// default to shut up terminal line noise
cflag_t cflag = CFLAG_NONE;
static int iflag = 0, interactive = 0, pflag_seen = 0, rflag = 0, tflag = 0;
static bool daemonized_tracer = 0;
/* Sometimes we want to print only succeeding syscalls. */
int not_failing_only = 0;
static int exit_code = 0;
static int strace_child = 0;
static char *username = NULL;
uid_t run_uid;
gid_t run_gid;
int acolumn = DEFAULT_ACOLUMN;
int max_strlen = DEFAULT_STRLEN;
static char *outfname = NULL;
FILE *outf;
static int curcol;
struct tcb **tcbtab;
unsigned int nprocs, tcbtabsize;
const char *progname;
extern char **environ;
static int detach(struct tcb *tcp, int sig);
static int trace(void);
static void cleanup(void);
static void interrupt(int sig);
static sigset_t empty_set, blocked_set;
#ifdef HAVE_SIG_ATOMIC_T
static volatile sig_atomic_t interrupted;
#else /* !HAVE_SIG_ATOMIC_T */
static volatile int interrupted;
#endif /* !HAVE_SIG_ATOMIC_T */
#ifdef USE_PROCFS
static struct tcb *pfd2tcb(int pfd);
static void reaper(int sig);
static void rebuild_pollv(void);
static struct pollfd *pollv;
#ifndef HAVE_POLLABLE_PROCFS
static void proc_poll_open(void);
static void proc_poller(int pfd);
struct proc_pollfd {
int fd;
int revents;
int pid;
};
static int poller_pid;
static int proc_poll_pipe[2] = { -1, -1 };
#endif /* !HAVE_POLLABLE_PROCFS */
#ifdef HAVE_MP_PROCFS
#define POLLWANT POLLWRNORM
#else
#define POLLWANT POLLPRI
#endif
#endif /* USE_PROCFS */
static void
usage(ofp, exitval)
FILE *ofp;
int exitval;
{
if (CDE_exec_mode) {
fprintf(ofp,
"CDE: Code, Data, and Environment packaging for Linux "
"Copyright 2010-2012 Philip Guo (philip@pgbovine.net) "
"Full user manual: http://www.pgbovine.net/cde.html "
"Basic usage: cde-exec [command within cde-root/ to run] ");
fprintf(ofp, " Options ");
fprintf(ofp, " -l : Use native dynamic linker on machine (less portable but more robust) ");
fprintf(ofp, " -n : Block network access (blank out bind/connect syscalls) ");
fprintf(ofp, " -f : Do NOT follow forks, so child processes run natively ");
fprintf(ofp, " -s : Streaming mode (ooh, mysterious!) ");
fprintf(ofp, " -i '<file path>' : Ignore the given exact file path ");
fprintf(ofp, " -p '<file path>' : Ignore the given file path prefix ");
fprintf(ofp, " -v : Verbose mode (for debugging) ");
fprintf(ofp, " -V : Print version ");
}
else {
fprintf(ofp,
"CDE: Code, Data, and Environment packaging for Linux "
"Copyright 2010-2012 Philip Guo (philip@pgbovine.net) "
"Full user manual: http://www.pgbovine.net/cde.html "
"Basic usage: cde [command to run and package] ");
fprintf(ofp, " Options ");
fprintf(ofp, " -c : Print the order of files copied into the package in cde-copied-files.log ");
fprintf(ofp, " -o <output dir> : Set a custom output directory instead of "cde-package/" ");
fprintf(ofp, " -f : Do NOT follow forks, so child processes are not packaged ");
fprintf(ofp, " -i '<file path>' : Ignore the given exact file path ");
fprintf(ofp, " -p '<file path>' : Ignore the given file path prefix ");
fprintf(ofp, " -v : Verbose mode (for debugging) ");
fprintf(ofp, " -V : Print version ");
}
exit(exitval);
}
#ifdef SVR4
#ifdef MIPS
void
foobar()
{
}
#endif /* MIPS */
#endif /* SVR4 */
/* Glue for systems without a MMU that cannot provide fork() */
#ifdef HAVE_FORK
# define strace_vforked 0
#else
# define strace_vforked 1
# define fork() vfork()
#endif
static int
set_cloexec_flag(int fd)
{
int flags, newflags;
if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
{
fprintf(stderr, "%s: fcntl F_GETFD: %s ",
progname, strerror(errno));
return -1;
}
newflags = flags | FD_CLOEXEC;
if (flags == newflags)
return 0;
if (fcntl(fd, F_SETFD, newflags) < 0)
{
fprintf(stderr, "%s: fcntl F_SETFD: %s ",
progname, strerror(errno));
return -1;
}
return 0;
}
/*
* When strace is setuid executable, we have to swap uids
* before and after filesystem and process management operations.
*/
static void
swap_uid(void)
{
#ifndef SVR4
int euid = geteuid(), uid = getuid();
if (euid != uid && setreuid(euid, uid) < 0)
{
fprintf(stderr, "%s: setreuid: %s ",
progname, strerror(errno));
exit(1);
}
#endif
}
#if _LFS64_LARGEFILE
# define fopen_for_output fopen64
#else
# define fopen_for_output fopen
#endif
static FILE *
strace_fopen(const char *path, const char *mode)
{
FILE *fp;
swap_uid();
if ((fp = fopen_for_output(path, mode)) == NULL)
fprintf(stderr, "%s: can't fopen '%s': %s ",
progname, path, strerror(errno));
swap_uid();
if (fp && set_cloexec_flag(fileno(fp)) < 0)
{
fclose(fp);
fp = NULL;
}
return fp;
}
static int popen_pid = -1;
#ifndef _PATH_BSHELL
# define _PATH_BSHELL "/bin/sh"
#endif
/*
* We cannot use standard popen(3) here because we have to distinguish
* popen child process from other processes we trace, and standard popen(3)
* does not export its child's pid.
*/
static FILE *
strace_popen(const char *command)
{
int fds[2];
swap_uid();
if (pipe(fds) < 0)
{
fprintf(stderr, "%s: pipe: %s ",
progname, strerror(errno));
swap_uid();
return NULL;
}
if (set_cloexec_flag(fds[1]) < 0)
{
close(fds[0]);
close(fds[1]);
swap_uid();
return NULL;
}
if ((popen_pid = fork()) == -1)
{
fprintf(stderr, "%s: fork: %s ",
progname, strerror(errno));
close(fds[0]);
close(fds[1]);
swap_uid();
return NULL;
}
if (popen_pid)
{
/* parent */
close(fds[0]);
swap_uid();
return fdopen(fds[1], "w");
} else
{
/* child */
close(fds[1]);
if (fds[0] && (dup2(fds[0], 0) || close(fds[0])))
{
fprintf(stderr, "%s: dup2: %s ",
progname, strerror(errno));
_exit(1);
}
execl(_PATH_BSHELL, "sh", "-c", command, NULL);
fprintf(stderr, "%s: execl: %s: %s ",
progname, _PATH_BSHELL, strerror(errno));
_exit(1);
}
}
static int
newoutf(struct tcb *tcp)
{
if (outfname && followfork > 1) {
char name[520 + sizeof(int) * 3];
FILE *fp;
sprintf(name, "%.512s.%u", outfname, tcp->pid);
if ((fp = strace_fopen(name, "w")) == NULL)
return -1;
tcp->outf = fp;
}
return 0;
}
static void
startup_attach(void)
{
int tcbi;
struct tcb *tcp;
/*
* Block user interruptions as we would leave the traced
* process stopped (process state T) if we would terminate in
* between PTRACE_ATTACH and wait4 () on SIGSTOP.
* We rely on cleanup () from this point on.
*/
if (interactive)
sigprocmask(SIG_BLOCK, &blocked_set, NULL);
if (daemonized_tracer) {
pid_t pid = fork();
if (pid < 0) {
_exit(1);
}
if (pid) { /* parent */
/*
* Wait for child to attach to straced process
* (our parent). Child SIGKILLs us after it attached.
* Parent's wait() is unblocked by our death,
* it proceeds to exec the straced program.
*/
pause();
_exit(0); /* paranoia */
}
}
for (tcbi = 0; tcbi < tcbtabsize; tcbi++) {
tcp = tcbtab[tcbi];
if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
continue;
#ifdef LINUX
if (tcp->flags & TCB_CLONE_THREAD)
continue;
#endif
/* Reinitialize the output since it may have changed. */
tcp->outf = outf;
if (newoutf(tcp) < 0)
exit(1);
#ifdef USE_PROCFS
if (proc_open(tcp, 1) < 0) {
fprintf(stderr, "trouble opening proc file ");
droptcb(tcp);
continue;
}
#else /* !USE_PROCFS */
# ifdef LINUX
if (followfork && !daemonized_tracer) {
char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3];
DIR *dir;
sprintf(procdir, "/proc/%d/task", tcp->pid);
dir = opendir(procdir);
if (dir != NULL) {
unsigned int ntid = 0, nerr = 0;
struct dirent *de;
int tid;
while ((de = readdir(dir)) != NULL) {
if (de->d_fileno == 0)
continue;
tid = atoi(de->d_name);
if (tid <= 0)
continue;
++ntid;
if (ptrace(PTRACE_ATTACH, tid, (char *) 1, 0) < 0)
++nerr;
else if (tid != tcbtab[tcbi]->pid) {
tcp = alloctcb(tid);
tcp->flags |= TCB_ATTACHED|TCB_CLONE_THREAD|TCB_FOLLOWFORK;
tcbtab[tcbi]->nchildren++;
tcbtab[tcbi]->nclone_threads++;
tcp->parent = tcbtab[tcbi];
CDE_init_tcb_dir_fields(tcp); // pgbovine - do it AFTER you init parent
}
if (interactive) {
sigprocmask(SIG_SETMASK, &empty_set, NULL);
if (interrupted)
return;
sigprocmask(SIG_BLOCK, &blocked_set, NULL);
}
}
closedir(dir);
ntid -= nerr;
if (ntid == 0) {
perror("attach: ptrace(PTRACE_ATTACH, ...)");
droptcb(tcp);
continue;
}
if (!qflag) {
fprintf(stderr, ntid > 1
? "Process %u attached with %u threads - interrupt to quit "
: "Process %u attached - interrupt to quit ",
tcbtab[tcbi]->pid, ntid);
}
continue;
} /* if (opendir worked) */
} /* if (-f) */
# endif
if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
perror("attach: ptrace(PTRACE_ATTACH, ...)");
droptcb(tcp);
continue;
}
/* INTERRUPTED is going to be checked at the top of TRACE. */
if (daemonized_tracer) {
/*
* It is our grandparent we trace, not a -p PID.
* Don't want to just detach on exit, so...
*/
tcp->flags &= ~TCB_ATTACHED;
/*
* Make parent go away.
* Also makes grandparent's wait() unblock.
*/
kill(getppid(), SIGKILL);
}
#endif /* !USE_PROCFS */
if (!qflag)
fprintf(stderr,
"Process %u attached - interrupt to quit ",
tcp->pid);
}
if (interactive)
sigprocmask(SIG_SETMASK, &empty_set, NULL);
}
static void
startup_child (char **argv)
{
struct stat statbuf;
const char *filename;
char pathname[MAXPATHLEN];
int pid = 0;
struct tcb *tcp;
// pgbovine
char path_to_search[MAXPATHLEN];
path_to_search[0] = '';
filename = argv[0];
if (strchr(filename, '/')) {
if (strlen(filename) > sizeof pathname - 1) {
errno = ENAMETOOLONG;
perror("strace: exec");
exit(1);
}
strcpy(pathname, filename);
}
#ifdef USE_DEBUGGING_EXEC
/*
* Debuggers customarily check the current directory
* first regardless of the path but doing that gives
* security geeks a panic attack.
*/
else if (stat(filename, &statbuf) == 0)
strcpy(pathname, filename);
#endif /* USE_DEBUGGING_EXEC */
else {
const char *path;
int m, n, len;
for (path = getenv("PATH"); path && *path; path += m) {
if (strchr(path, ':')) {
n = strchr(path, ':') - path;
m = n + 1;
}
else
m = n = strlen(path);
if (n == 0) {
if (!getcwd(pathname, MAXPATHLEN))
continue;
len = strlen(pathname);
}
else if (n > sizeof pathname - 1)
continue;
else {
strncpy(pathname, path, n);
len = n;
}
if (len && pathname[len - 1] != '/')
pathname[len++] = '/';
strcpy(pathname + len, filename);
// pgbovine
if (CDE_exec_mode) {
strcpy_redirected_cderoot(path_to_search, pathname);
}
else {
strcpy(path_to_search, pathname);
}
//printf("path_to_search = '%s' ", path_to_search);
if (stat(path_to_search, &statbuf) == 0 &&
/* Accept only regular files
with some execute bits set.
XXX not perfect, might still fail */
S_ISREG(statbuf.st_mode) &&
(statbuf.st_mode & 0111))
break;
}
}
// pgbovine - if we still haven't initialized it yet, do so now
if (path_to_search[0] == '') { // uninit
if (CDE_exec_mode) {
strcpy_redirected_cderoot(path_to_search, pathname);
}
else {
strcpy(path_to_search, pathname);
}
}
if (stat(path_to_search, &statbuf) < 0) {
fprintf(stderr, "%s: %s: command not found (path_to_search=%s) ",
progname, filename, path_to_search);
exit(1);
}
strace_child = pid = fork();
if (pid < 0) {
perror("strace: fork");
cleanup();
exit(1);
}
if ((pid != 0 && daemonized_tracer) /* parent: to become a traced process */
|| (pid == 0 && !daemonized_tracer) /* child: to become a traced process */
) {
pid = getpid();
#ifdef USE_PROCFS
if (outf != stderr) close (fileno (outf));
#ifdef MIPS
/* Kludge for SGI, see proc_open for details. */
sa.sa_handler = foobar;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
#endif /* MIPS */
#ifndef FREEBSD
pause();
#else /* FREEBSD */
kill(pid, SIGSTOP); /* stop HERE */
#endif /* FREEBSD */
#else /* !USE_PROCFS */
if (outf!=stderr)
close(fileno (outf));
if (!daemonized_tracer) {
if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
perror("strace: ptrace(PTRACE_TRACEME, ...)");
exit(1);
}
if (debug)
kill(pid, SIGSTOP);
}
if (username != NULL || geteuid() == 0) {
uid_t run_euid = run_uid;
gid_t run_egid = run_gid;
if (statbuf.st_mode & S_ISUID)
run_euid = statbuf.st_uid;
if (statbuf.st_mode & S_ISGID)
run_egid = statbuf.st_gid;
/*
* It is important to set groups before we
* lose privileges on setuid.
*/
if (username != NULL) {
if (initgroups(username, run_gid) < 0) {
perror("initgroups");
exit(1);
}
if (setregid(run_gid, run_egid) < 0) {
perror("setregid");
exit(1);
}
if (setreuid(run_uid, run_euid) < 0) {
perror("setreuid");
exit(1);
}
}
}
else
setreuid(run_uid, run_uid);
if (!daemonized_tracer) {
/*
* Induce an immediate stop so that the parent
* will resume us with PTRACE_SYSCALL and display
* this execve call normally.
* Unless of course we're on a no-MMU system where
* we vfork()-ed, so we cannot stop the child.
*/
if (!strace_vforked)
kill(getpid(), SIGSTOP);
} else {
struct sigaction sv_sigchld;
sigaction(SIGCHLD, NULL, &sv_sigchld);
/*
* Make sure it is not SIG_IGN, otherwise wait
* will not block.
*/
signal(SIGCHLD, SIG_DFL);
/*
* Wait for grandchild to attach to us.
* It kills child after that, and wait() unblocks.
*/
alarm(3);
wait(NULL);
alarm(0);
sigaction(SIGCHLD, &sv_sigchld, NULL);
}
#endif /* !USE_PROCFS */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.