Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 21.5 Source Code 833


641 /*
642 * Check for a change in the config file.
643 */
644 pthread_mutex_lock(&configlock);
645 if (reread) {
646 freeaddrinfo(printer);
647 printer=NULL;
648 printer_name=NULL;
649 reread =0;
650 pthread_mutex_unlock(&configlock);
651 init_printer();
652 } else {
653 pthread_mutex_unlock(&configlock);
654 }
655 /*
656 * Send job to printer.
657 */
658 sprintf(name, "%s/%s/%d", SPOOLDIR, DATADIR, jp->jobid);
659 if ((fd = open(name, O_RDONLY)) < 0) {
660 log_msg("job %d canceled - can’t open %s: %s",
661 jp->jobid, name, strerror(errno));
662 free(jp);
663 continue;
664 }
665 if (fstat(fd, &sbuf) < 0) {
666 log_msg("job %d canceled - can’t fstat %s: %s",
667 jp->jobid, name, strerror(errno));
668 free(jp);
669 close(fd);
670 continue;
671 }

[641 – 654] Now that we have a job to print, we check for a change in the configuration
file. Welock theconfiglockmutex and check therereadvariable. If it is
nonzero, then we free the old printeraddrinfolist, clear the pointers,
unlock the mutex, and call init_printer to reinitialize the printer
information. Since only this context looks at and potentially changes the
printer information after themainthread initialized it, we don’t need any
synchronization other than using theconfiglockmutex to protect the
state of therereadflag.
Note that although we acquireand release two different mutex locks in this
function, we never hold both at the same time, so we don’t need to establish
alock hierarchy (Section 11.6.2).
[655 – 671] If we can’t open the data file, we log an error message, free the job
structure, and continue. After opening the file, we callfstatto find the
size of the file. If this fails, we log an error message, clean up, and continue.
Free download pdf