]> andersk Git - moira.git/blob - dcm/utils.c
don't send a zgram on every startup
[moira.git] / dcm / utils.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 by the Massachusetts Institute of Technology
7  * 
8  *      Utility functions used by the DCM.
9  */
10
11 #ifndef lint
12 static char *rcsid_utils_c = "$Header$";
13 #endif lint
14
15 #include <stdio.h>
16 #include <strings.h>
17 #include <varargs.h>
18 #include <sys/types.h>
19 #include <sys/time.h>
20 #include <sys/file.h>
21 #include <zephyr/zephyr.h>
22 #include <sms.h>
23 #include "dcm.h"
24
25
26 int dbg = 0;
27 int log_flags;
28
29 extern char *strsave();
30
31 void dcm_com_err_hook(whoami, code, fmt, pvar)
32         char *whoami;
33         int code;
34         char *fmt;
35         caddr_t pvar;
36 {
37         if (whoami) {
38                 fputs(whoami, stderr);
39                 fputs(": ", stderr);
40         }
41         if (code) {
42                 fputs(error_message(code), stderr);
43         }
44         if (fmt) {
45                 _doprnt(fmt, pvar, stderr);
46         }
47         putc('\n', stderr);
48         fflush(stderr);
49 }
50                 
51 void leave(s)
52 char *s;
53 {
54     extern int errno;
55
56     if (*s)
57       com_err(whoami, errno, "%s: exiting", s);
58     else
59       com_err(whoami, errno, "exiting");
60
61     exit(errno);
62 }
63
64 void scream(argc, argv, hint)
65 int argc;
66 char **argv;
67 int hint;
68 {
69     leave("Programmer botch");
70 }
71
72
73 char *itoa(i)
74 int i;
75 {
76     char buf[20];
77
78     sprintf(buf, "%d", i);
79     return(strsave(buf));
80 }
81
82
83 char *tkt_string()
84 {
85     return("/tmp/tkt_dcm");
86 }
87
88
89 int maybe_lock_update(dir, host, service, exclusive)
90 char *dir, *host, *service;
91 int exclusive;
92 {
93     char lock[BUFSIZ];
94     int fd;
95
96     sprintf(lock, "%s/dcm/locks/%s.%s", dir, host, service);
97     if ((fd = open(lock, O_TRUNC |  O_CREAT, 0)) < 0)
98       com_err(whoami, errno, ": maybe_lock_update: opening %s", lock);
99     else if (flock(fd, (exclusive ? LOCK_EX : LOCK_SH) | LOCK_NB) != 0) {
100         if (errno != EWOULDBLOCK) 
101           com_err(whoami, errno, ": maybe_lock_update: flock");
102         else if (dbg & DBG_VERBOSE)
103           com_err(whoami, 0, "%s already locked\n", lock);
104         close(fd);
105         return -1;
106     } else if (dbg & DBG_VERBOSE) 
107       com_err(whoami, 0, "%s now locked\n", lock);
108     return fd;
109 }
110
This page took 0.139914 seconds and 5 git commands to generate.