/* * Copyright (c) 1995, 1996 Gunther Schadow. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "pg_config.h" #include "fsyslog.h" #include "logfile.h" #include "trip.h" #include "trap.h" #include #include #include #ifdef WITH_DELAY # define slp usleep(rand() % 100000 + 10000); #else # define slp #endif # define LOGFILE "./triptrap.log"; void trip(pid_t trap); /* high priority */ void trap(pid_t trip); /* low priority */ void reaper(int sig); int main() { pid_t trip_pid; pid_t trap_pid; /* logfile = LOGFILE; */ ssignal(SIGCHLD,reaper,0); trap_pid = getpid(); trip_pid = fork(); if(trip_pid < 0) { perror("fork for trip:"); exit(1); } else if(trip_pid == 0) trip(trap_pid); /* child */ else trap(trip_pid); return 0; } void trip(pid_t trap) { bool __init_trip = TRUE ; openlog("trip", LOG_NDELAY | LOG_PID, LOG_LPR); DBG(syslog(LOG_DEBUG,"I am trip and trap is %d", trap)); atsignal(SIGCLAIM, 0) { DBG(syslog(LOG_DEBUG,"yield line to trap")); yield_line(trap); DBG(syslog(LOG_DEBUG,"entering wait state")); wait_line(); } atsignal(SIGYIELD, 0) { DBG(syslog(LOG_DEBUG,"released from wait state")); } if(__init_trip) { __init_trip = FALSE; DBG(syslog(LOG_DEBUG,"entering wait state (init)")); wait_line(); } while(TRUE) { DBG(syslog(LOG_DEBUG,"waiting ...")); slp; DBG(syslog(LOG_DEBUG,"claim the line")); claim_line(trap, PRI_HIGH); syslog(LOG_DEBUG, "have the line"); fprintf(stderr, "*** TRIP ***\n"); DBG(syslog(LOG_DEBUG,"working ...")); slp; DBG(syslog(LOG_DEBUG,"release the line")); yield_line(trap); } } void trap(pid_t trip) { bool __init_trap = TRUE; openlog("trap", LOG_NDELAY | LOG_PID, LOG_LPR); DBG(syslog(LOG_DEBUG,"I am trap and trap is %d", trip)); atsignal(SIGCLAIM, 0) { DBG(syslog(LOG_DEBUG,"yield line to trip")); yield_line(trip); DBG(syslog(LOG_DEBUG,"entering wait state")); wait_line(); } atsignal(SIGYIELD, 0) { DBG(syslog(LOG_DEBUG,"released from wait state")); } if(__init_trap) { __init_trap = FALSE; DBG(syslog(LOG_DEBUG,"initializing ...")); sleep(1); yield_line( trip ); } while(TRUE) { DBG(syslog(LOG_DEBUG,"waiting ...")); slp; DBG(syslog(LOG_DEBUG,"claim the line")); claim_line(trip, PRI_LOW); syslog(LOG_DEBUG, "have the line"); fprintf(stderr, "*** TRAP ***\n"); DBG(syslog(LOG_DEBUG,"working ...")); slp; DBG(syslog(LOG_DEBUG,"release the line")); yield_line(trip); } } void reaper(int sig) { int status; pid_t pid; DBG(syslog(LOG_DEBUG, "reaper called by signal %d `%s'", sig, sys_siglist[sig])); while((pid = wait3(&status, WNOHANG, 0)) > 0) { if(WIFEXITED(status)) { DBG(syslog(LOG_DEBUG, "process %d exited with %d", pid, WEXITSTATUS(status))); } else if(WIFSIGNALED(status)) { int tsig = WTERMSIG(status); DBG(syslog(LOG_DEBUG, "process %d received signal %d `%s'", pid, tsig, sys_siglist[tsig])); } else if(WIFSTOPPED(status)) { int ssig = WSTOPSIG(status); DBG(syslog(LOG_DEBUG, "process %d stopped by signal %d `s'", pid, ssig, sys_siglist[ssig])); kill(pid, SIGCONT); } } exit(2); }