Obfuscate RCS ID matching so that CVS doesn't expand it.
[netbsd-mini2440.git] / dist / ntp / sntp / unix.c
blob4238ccc7a3e527d041b581a84c57d977c600fad7
1 /* $NetBSD$ */
3 /* Copyright (C) 1996 N.M. Maclaren
4 Copyright (C) 1996 The University of Cambridge
6 This includes code that really should have been part of ANSI/ISO C, but was
7 left out for historical reasons (despite requests to define ftty), plus
8 the get_lock() and log_message() functions.
9 */
11 #include "header.h"
13 #include <sys/types.h>
14 #include <unistd.h>
15 #include <syslog.h>
16 #include <signal.h>
18 #define UNIX
19 #include "kludges.h"
20 #undef UNIX
23 void do_nothing (int seconds) {
25 /* Wait for a fixed period, possibly uninterruptibly. This should not wait
26 for less than the specified period, if that can be avoided. */
28 sleep((unsigned int)(seconds+2)); /* +2 is enough for POSIX */
33 int ftty (FILE *file) {
35 /* Return whether the file is attached to an interactive device. */
37 return isatty(fileno(file));
42 void set_lock (int lock) {
44 /* Check that we have enough privileges to reset the time and that no other
45 updating msntp process is running, but don't bother with fancy interlocking.
46 This function is really only to permit the daemon mode to be restarted in a
47 cron job and improve the diagnostics; it can be replaced by a 'return'
48 statement if it causes implementation difficulties. Note that there is little
49 point in clearing the lock under Unix, but do so anyway. */
51 FILE *file;
52 long pid;
54 if (lockname == NULL || lockname[0] == '\0') return;
55 if (lock) {
56 errno = 0;
57 if ((file = fopen(lockname,"r")) != NULL &&
58 fscanf(file,"%ld",&pid) == 1 && kill(pid,0) == 0) {
59 if (verbose || isatty(STDIN_FILENO) || isatty(STDOUT_FILENO))
60 fatal(0,"another msntp process is currently running",NULL);
61 else
62 fatal(0,NULL,NULL);
64 if (file != NULL) fclose(file);
65 errno = 0;
66 if ((file = fopen(lockname,"w")) == NULL ||
67 fprintf(file,"%ld\n",(long)getpid()) <= 0 ||
68 ferror(file) || fclose(file) != 0)
69 fatal(1,"unable to write PID to %s",lockname);
70 adjust_time(0.0,1,0.0);
71 } else {
72 errno = 0;
73 if (remove(lockname) != 0)
74 fatal(1,"unable to remove the msntp lockname %s",lockname);
81 * Log a message, crudely.
82 * This is used in only one place, but could be used more widely.
85 void
86 log_message (const char *message)
89 syslog(
90 #ifdef LOG_DAEMON
91 LOG_DAEMON |
92 #endif
93 LOG_WARNING, "%s", message);