]>
Commit | Line | Data |
---|---|---|
846841f4 | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
846841f4 | 6 | * |
7 | * Utility functions used by the DCM. | |
0a5ff702 | 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>. | |
846841f4 | 12 | */ |
13 | ||
14 | #ifndef lint | |
15 | static char *rcsid_utils_c = "$Header$"; | |
16 | #endif lint | |
17 | ||
0a5ff702 | 18 | #include <mit-copyright.h> |
846841f4 | 19 | #include <stdio.h> |
20 | #include <strings.h> | |
f5c191c8 | 21 | #include <errno.h> |
846841f4 | 22 | #include <varargs.h> |
23 | #include <sys/types.h> | |
24 | #include <sys/time.h> | |
25 | #include <sys/file.h> | |
15fa49b5 | 26 | #include <sys/stat.h> |
27 | #include <fcntl.h> | |
2ce085d2 | 28 | #include <moira.h> |
29 | #include <moira_site.h> | |
846841f4 | 30 | #include "dcm.h" |
31 | ||
32 | ||
33 | int dbg = 0; | |
34 | int log_flags; | |
35 | ||
36 | extern char *strsave(); | |
37 | ||
38 | void dcm_com_err_hook(whoami, code, fmt, pvar) | |
39 | char *whoami; | |
40 | int code; | |
41 | char *fmt; | |
42 | caddr_t pvar; | |
43 | { | |
44 | if (whoami) { | |
45 | fputs(whoami, stderr); | |
46 | fputs(": ", stderr); | |
47 | } | |
48 | if (code) { | |
49 | fputs(error_message(code), stderr); | |
50 | } | |
51 | if (fmt) { | |
52 | _doprnt(fmt, pvar, stderr); | |
53 | } | |
54 | putc('\n', stderr); | |
55 | fflush(stderr); | |
56 | } | |
57 | ||
58 | void leave(s) | |
59 | char *s; | |
60 | { | |
61 | extern int errno; | |
62 | ||
63 | if (*s) | |
64 | com_err(whoami, errno, "%s: exiting", s); | |
65 | else | |
66 | com_err(whoami, errno, "exiting"); | |
67 | ||
68 | exit(errno); | |
69 | } | |
70 | ||
71 | void scream(argc, argv, hint) | |
72 | int argc; | |
73 | char **argv; | |
74 | int hint; | |
75 | { | |
76 | leave("Programmer botch"); | |
77 | } | |
78 | ||
79 | ||
80 | char *itoa(i) | |
81 | int i; | |
82 | { | |
83 | char buf[20]; | |
84 | ||
85 | sprintf(buf, "%d", i); | |
86 | return(strsave(buf)); | |
87 | } | |
88 | ||
89 | ||
90 | char *tkt_string() | |
91 | { | |
92 | return("/tmp/tkt_dcm"); | |
93 | } | |
94 | ||
95 | ||
185f76ce | 96 | int maybe_lock_update(host, service, exclusive) |
97 | char *host, *service; | |
846841f4 | 98 | int exclusive; |
99 | { | |
100 | char lock[BUFSIZ]; | |
101 | int fd; | |
15fa49b5 | 102 | flock_t fl; |
846841f4 | 103 | |
185f76ce | 104 | sprintf(lock, "%s/%s.%s", LOCK_DIR, host, service); |
15fa49b5 | 105 | fl.l_type = exclusive ? F_WRLCK : F_RDLCK; |
106 | fl.l_whence = fl.l_start = fl.l_len = 0; | |
107 | if ((fd = open(lock, O_TRUNC | O_CREAT | O_RDWR, 0)) < 0) | |
846841f4 | 108 | com_err(whoami, errno, ": maybe_lock_update: opening %s", lock); |
15fa49b5 | 109 | else if (fcntl(fd, F_SETLK, &fl) != 0) { |
110 | if (errno != EAGAIN) | |
846841f4 | 111 | com_err(whoami, errno, ": maybe_lock_update: flock"); |
112 | else if (dbg & DBG_VERBOSE) | |
113 | com_err(whoami, 0, "%s already locked\n", lock); | |
114 | close(fd); | |
115 | return -1; | |
116 | } else if (dbg & DBG_VERBOSE) | |
117 | com_err(whoami, 0, "%s now locked\n", lock); | |
118 | return fd; | |
119 | } | |
120 | ||
753d180d | 121 | |
2ce085d2 | 122 | int mr_query_with_retry(name, argc, argv, proc, hint) |
753d180d | 123 | char *name; |
124 | int argc; | |
125 | char **argv; | |
126 | int (*proc)(); | |
127 | char *hint; | |
128 | { | |
129 | int status, tries; | |
130 | ||
131 | for (tries = 0; tries < DEADLOCK_TRIES; tries++) { | |
2ce085d2 | 132 | status = mr_query(name, argc, argv, proc, hint); |
133 | if (status != MR_DEADLOCK) | |
753d180d | 134 | return(status); |
135 | sleep(DEADLOCK_WAIT); | |
136 | } | |
137 | return(status); | |
138 | } |