From owner-freebsd-hackers Wed Feb 5 11:51:41 1997 Return-Path: Received: (from root@localhost) by freefall.freebsd.org (8.8.5/8.8.5) id LAA06961 for hackers-outgoing; Wed, 5 Feb 1997 11:51:41 -0800 (PST) Received: from sax.sax.de (sax.sax.de [193.175.26.33]) by freefall.freebsd.org (8.8.5/8.8.5) with SMTP id LAA06953 for ; Wed, 5 Feb 1997 11:51:33 -0800 (PST) Received: (from uucp@localhost) by sax.sax.de (8.6.12/8.6.12-s1) with UUCP id UAA15545; Wed, 5 Feb 1997 20:51:05 +0100 Received: (from j@localhost) by uriah.heep.sax.de (8.8.5/8.6.9) id UAA11573; Wed, 5 Feb 1997 20:48:15 +0100 (MET) Message-ID: Date: Wed, 5 Feb 1997 20:48:15 +0100 From: j@uriah.heep.sax.de (J Wunsch) To: ts@polynet.lviv.ua Cc: stevel@logues.rhn.orst.edu, FreeBSD-hackers@freebsd.org Subject: Re: UPS daemon References: <199702051719.TAA18357@NetSurfer.lp.lviv.ua> X-Mailer: Mutt 0.55-PL10 Mime-Version: 1.0 X-Phone: +49-351-2012 669 X-PGP-Fingerprint: DC 47 E6 E4 FF A6 E9 8F 93 21 E0 7D F9 12 D6 4E Reply-To: joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch) In-Reply-To: <199702051719.TAA18357@NetSurfer.lp.lviv.ua>; from Slavik Terletsky on Feb 5, 1997 19:21:54 +0000 Sender: owner-hackers@freebsd.org X-Loop: FreeBSD.org Precedence: bulk As Slavik Terletsky wrote: > I managed to hack upsd. As most of ppl who has APC Smart UPS I use > it in a dumb mode (I had no info on the cabling UPS to my box). Ummm. You've reinvented the wheel. There's already a nice upsd available, and it does indeed use the Smart UPS in smart mode (which is *way* preferable over dumb mode, since those beasts are really verbose in their reporting). See ftp.ww.net. Also, sorry to beat at you, but your program has many style violations (not so terrible) and even some more serious problems. > char *scr = "/etc/rc.shutdown"; Don't use this. Expect /etc/rc.shutdown to become a standard component of FreeBSD anytime soon, to be called by init before shutting down the system. > /* check if script exists */ > if(!(fd = fopen(scr, "r"))) { > fprintf(stderr, "fopen: %s: %s\n", scr, sys_errlist[errno]); > exit(1); > } > fclose(fd); `fd' translates to `file descriptor' for many people, which is something very different from an stdio file handle (of type `FILE *'). Use access(2) or stat(2) to see if a file exists, abusing stdio is overkill. Better use the err(3) function family instead of hacking your own. Code using sys_errlist[] all over the place is probably stylistically bad. (If at all, use a centralized error handling function for better portability.) > /* become a daemon */ > switch(fork()) { > case -1: /* error */ > fprintf(stderr, "fork: %s\n", sys_errlist[errno]); > exit(1); > case 0: /* child */ > break; > default: /* parent */ > exit(0); > } Hmm, but you don't actually become a daemon. This would require calling setsid(2). > /* monitor device */ > while(1) { ... > sleep(1); > } Uh-oh. This is indeed a system hog. You examine the line status once every second, thus preventing that daemon from being swapped. A much better way is to have it making the line its controlling tty, and arrange for the external logic dropping DCD to get the attention of the process (i.e., when power fails). This way, the daemon can sleep all the time (and can be swapped), but will be signalled with a SIGHUP to wakeup. > /* save our filesystem */ > system("/bin/sync"); > system("/bin/sync"); > system("/bin/sync"); Hmpf. :-) Did you ever look into /bin/sync? All it does is calling sync(2). Anyway, there's no need to call it at all. Leave this to init. ``Thou shalt sync thrice.'' is a very old myth, but it's pure religion, nothing else. ;-) > /* wait and then run script */ > sleep(wait); > system(scr); Also, send the appropriate signal to init instead. Again, sorry for hacking on your code, please take my ramblings as constructive criticism, not as offense. -- cheers, J"org joerg_wunsch@uriah.heep.sax.de -- http://www.sax.de/~joerg/ -- NIC: JW11-RIPE Never trust an operating system you don't have sources for. ;-)