Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

830 Communicating with a Network Printer Chapter 21


549 /*
550 * Deal with signals.
551 *
552 * LOCKING: acquires and releases configlock.
553 */
554 void *
555 signal_thread(void *arg)
556 {
557 int err, signo;

558 for (;;) {
559 err=sigwait(&mask, &signo);
560 if (err != 0)
561 log_quit("sigwait failed: %s", strerror(err));
562 switch (signo) {
563 case SIGHUP:
564 /*
565 * Schedule to re-read the configuration file.
566 */
567 pthread_mutex_lock(&configlock);
568 reread =1;
569 pthread_mutex_unlock(&configlock);
570 break;

571 case SIGTERM:
572 kill_workers();
573 log_msg("terminate with signal %s", strsignal(signo));
574 exit(0);

575 default:
576 kill_workers();
577 log_quit("unexpected signal %d", signo);
578 }
579 }
580 }

[549 – 562] Thesignal_threadfunction is run by the thread that is responsible for
handling signals. In themainfunction, we initialized the signal mask to
includeSIGHUPandSIGTERM.Here, we callsigwaitto wait for one of
these signals to occur.Ifsigwaitfails, we log an error message and exit.
[563 – 570] If we receiveSIGHUP, we acquiretheconfiglockmutex, set thereread
variable to 1, and release the mutex. This tells the printer daemon to reread
the configuration file on the next iteration in its processing loop.
[571 – 574] If we receiveSIGTERM,wecall kill_workersto kill all the worker
threads, log a message, and callexitto terminate the process.
[575 – 580] If we receive a signal we arenot expecting, we kill the worker threads and
calllog_quitto log an error message and exit.
Free download pdf