]> andersk Git - moira.git/blob - dcm/utils.c
changed error code when generator coredumps
[moira.git] / dcm / utils.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  * 
7  *      Utility functions used by the DCM.
8  *
9  *  (c) Copyright 1987, 1988 by the Massachusetts Institute of Technology.
10  *  For copying and distribution information, please see the file
11  *  <mit-copyright.h>.
12  */
13
14 #ifndef lint
15 static char *rcsid_utils_c = "$Header$";
16 #endif lint
17
18 #include <mit-copyright.h>
19 #include <stdio.h>
20 #include <strings.h>
21 #include <errno.h>
22 #include <varargs.h>
23 #include <sys/types.h>
24 #include <sys/time.h>
25 #include <sys/file.h>
26 #include <moira.h>
27 #include <moira_site.h>
28 #include "dcm.h"
29
30
31 int dbg = 0;
32 int log_flags;
33
34 extern char *strsave();
35
36 void dcm_com_err_hook(whoami, code, fmt, pvar)
37         char *whoami;
38         int code;
39         char *fmt;
40         caddr_t pvar;
41 {
42         if (whoami) {
43                 fputs(whoami, stderr);
44                 fputs(": ", stderr);
45         }
46         if (code) {
47                 fputs(error_message(code), stderr);
48         }
49         if (fmt) {
50                 _doprnt(fmt, pvar, stderr);
51         }
52         putc('\n', stderr);
53         fflush(stderr);
54 }
55                 
56 void leave(s)
57 char *s;
58 {
59     extern int errno;
60
61     if (*s)
62       com_err(whoami, errno, "%s: exiting", s);
63     else
64       com_err(whoami, errno, "exiting");
65
66     exit(errno);
67 }
68
69 void scream(argc, argv, hint)
70 int argc;
71 char **argv;
72 int hint;
73 {
74     leave("Programmer botch");
75 }
76
77
78 char *itoa(i)
79 int i;
80 {
81     char buf[20];
82
83     sprintf(buf, "%d", i);
84     return(strsave(buf));
85 }
86
87
88 char *tkt_string()
89 {
90     return("/tmp/tkt_dcm");
91 }
92
93
94 int maybe_lock_update(host, service, exclusive)
95 char *host, *service;
96 int exclusive;
97 {
98     char lock[BUFSIZ];
99     int fd;
100
101     sprintf(lock, "%s/%s.%s", LOCK_DIR, host, service);
102     if ((fd = open(lock, O_TRUNC |  O_CREAT, 0)) < 0)
103       com_err(whoami, errno, ": maybe_lock_update: opening %s", lock);
104     else if (flock(fd, (exclusive ? LOCK_EX : LOCK_SH) | LOCK_NB) != 0) {
105         if (errno != EWOULDBLOCK) 
106           com_err(whoami, errno, ": maybe_lock_update: flock");
107         else if (dbg & DBG_VERBOSE)
108           com_err(whoami, 0, "%s already locked\n", lock);
109         close(fd);
110         return -1;
111     } else if (dbg & DBG_VERBOSE) 
112       com_err(whoami, 0, "%s now locked\n", lock);
113     return fd;
114 }
115
116
117 int mr_query_with_retry(name, argc, argv, proc, hint)
118 char *name;
119 int argc;
120 char **argv;
121 int (*proc)();
122 char *hint;
123 {
124     int status, tries;
125
126     for (tries = 0; tries < DEADLOCK_TRIES; tries++) {
127         status = mr_query(name, argc, argv, proc, hint);
128         if (status != MR_DEADLOCK)
129           return(status);
130         sleep(DEADLOCK_WAIT);
131     }
132     return(status);
133 }
This page took 0.279411 seconds and 5 git commands to generate.