1#!/bin/sh 2 3# 4# Copyright (c) 2008 Peter Holm <pho@FreeBSD.org> 5# All rights reserved. 6# 7# Redistribution and use in source and binary forms, with or without 8# modification, are permitted provided that the following conditions 9# are met: 10# 1. Redistributions of source code must retain the above copyright 11# notice, this list of conditions and the following disclaimer. 12# 2. Redistributions in binary form must reproduce the above copyright 13# notice, this list of conditions and the following disclaimer in the 14# documentation and/or other materials provided with the distribution. 15# 16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26# SUCH DAMAGE. 27# 28 29# Test scenario by kib@ 30 31. ../default.cfg 32 33odir=`pwd` 34 35cd /tmp 36sed '1,/^EOF/d' < $odir/$0 > kevent4.c 37mycc -o kevent4 -Wall kevent4.c -pthread || exit 1 38rm -f kevent4.c 39 40cd $odir 41export runRUNTIME=3m 42(cd ..; ./run.sh > /dev/null 2>&1) & 43rpid=$! 44 45(cd $RUNDIR; /tmp/kevent4 $rpid > /dev/null) & 46 47sleep 120 48kill $rpid 49../tools/killall.sh > /dev/null 2>&1 50kill $! 51wait 52rm -f /tmp/kevent4 53 54exit 55EOF 56#include <sys/types.h> 57#include <sys/event.h> 58 59#include <unistd.h> 60#include <errno.h> 61#include <string.h> 62#include <stdio.h> 63#include <stdlib.h> 64#ifndef true 65# define true 1 66#endif 67 68static int kq; 69 70static void 71err(const char *msg, int err_no) 72{ 73 fprintf(stderr, "%s: %s\n", msg, strerror(err_no)); 74 exit(1); 75} 76 77static void 78init_kq() 79{ 80 kq = kqueue(); 81 if (kq == -1) 82 err("kqueue", errno); 83} 84 85static void 86add_watch(pid_t pid) 87{ 88 struct kevent kev; 89 90 bzero(&kev, sizeof(kev)); 91 kev.ident = pid; 92 kev.flags = EV_ADD | EV_ENABLE; 93 kev.filter = EVFILT_PROC; 94 kev.fflags = NOTE_EXIT | NOTE_FORK | NOTE_EXEC | NOTE_TRACK; 95 96 while (true) { 97 int res = kevent(kq, &kev, 1, NULL, 0, NULL); 98 if (res == -1) { 99 if (errno == EINTR) 100 continue; 101 if (errno == ESRCH) 102 break; 103 104 int err_no = errno; 105 char msg[64]; 106 snprintf(msg, sizeof(msg), 107 "kevent - add watch for pid %u", pid); 108 err(msg, err_no); 109 } 110 else 111 break; 112 } 113} 114 115static void polling() 116{ 117 struct kevent kev[10]; 118 pid_t pid; 119 int i; 120 121 while (true) { 122 bzero(&kev, sizeof(kev)); 123 int res = kevent(kq, NULL, 0, kev, 124 sizeof(kev) / sizeof(kev[0]), NULL); 125 if (res == -1) { 126 if (errno == EINTR) 127 continue; 128 129 if (errno == ESRCH) 130 continue; 131 132 err("kevent", errno); 133 } 134 135 for (i = 0; i < res; i++) { 136 pid = kev[i].ident; 137 if (kev[i].fflags & NOTE_CHILD) { 138 add_watch(pid); 139 printf("%u - new process, parent %u\n", pid, 140 (unsigned int)kev[i].data); 141 } 142 if (kev[i].fflags & NOTE_FORK) { 143 printf("%u forked\n", pid); 144 } 145 if (kev[i].fflags & NOTE_EXEC) { 146 printf("%u called exec\n", pid); 147 } 148 if (kev[i].fflags & NOTE_EXIT) { 149 printf("%u exited\n", pid); 150 } 151 if (kev[i].fflags & NOTE_TRACK) { 152 printf("%u forked - track\n", pid); 153 } 154 if (kev[i].fflags & NOTE_TRACKERR) { 155 fprintf(stderr, "%u - track error\n", pid); 156 } 157 } 158 } 159} 160 161int main(int argc, char *argv[]) 162{ 163 if (argc != 2) { 164 fprintf(stderr, "pid ?\n"); 165 return (2); 166 } 167 pid_t parent = atoi(argv[1]); 168 169 init_kq(); 170 add_watch(parent); 171 polling(); 172 173 return (0); 174} 175