]> andersk Git - moira.git/blob - dcm/dcm.c
3afa5defeb7a27a94985ce708a5fa991d8884f06
[moira.git] / dcm / dcm.c
1 /*
2  * The Data Control Manager for SMS.
3  *
4  * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
5  * For copying and distribution information, see the file
6  * "mit-copyright.h".
7  *
8  * $Source$
9  * $Author$
10  * $Header$
11  */
12
13 #ifndef lint
14 static char rcsid_dcm_c[] = "$Header$";
15 #endif lint
16
17 #include <stdio.h>
18 #include <update.h>
19 #include <sys/file.h>
20 #include <sys/time.h>
21 #include <sys/wait.h>
22 #include <ctype.h>
23 #include <sms.h>
24 #include <sms_app.h>
25 #include "dcm.h"
26 #include "mit-copyright.h"
27
28 extern char *ctime();
29 extern char *getenv();
30 extern int log_flags;
31 extern char *error_message();
32 char *itoa();
33 int gqval();
34 long time();
35
36
37 /* declared global so that we can get the current time from different places. */
38 struct timeval tv;
39
40
41 main(argc, argv)
42 int argc;
43 char *argv[];
44 {
45         int i;
46         char **arg = argv;
47         char *qargv[3];
48         int status;
49
50         whoami = argv[0];
51         dbg = atoi(getenv("DEBUG"));
52         umask(UMASK);
53         log_flags = 0;
54         setlinebuf(stderr);
55         setlinebuf(stdout);
56         
57         while(++arg - argv < argc) {
58             if (**arg == '-')
59                 switch((*arg)[1]) {
60                 case 'd':
61                     dbg =  atoi((*arg)[2]? *arg+2: *++arg);
62                     break;
63                 }
64         }
65         set_com_err_hook(dcm_com_err_hook);
66         
67         /* if /etc/nodcm exists, punt quietly. */
68         if (!access("/etc/nodcm", F_OK)) {
69                 exit(1);
70         } 
71
72         if (status = sms_connect()) {
73             com_err(whoami, status, " on sms_connect");
74             leave("connect failed");
75         }
76
77         if (status = sms_auth("dcm")) {
78             com_err(whoami, status, " on \"authenticate\"");
79             leave("auth failed");
80         }
81
82         /* if DCM is not enabled, exit after logging */
83         qargv[0] = "dcm_enable";
84         if (status = sms_query("get_value", 1, qargv, gqval, &i)) {
85             com_err(whoami, status, " check dcm_enable");
86             leave("query failed");
87         }
88         if (i == 0) {
89             errno = 0;
90             leave("dcm_enable not set");
91         } 
92
93         /* do it! */
94         do_services();
95         errno = 0;
96         leave("");
97 }
98
99
100 /* Used by the get_value query when checking for dcm_enable. */
101
102 gqval(argc, argv, hint)
103 int argc;
104 char **argv;
105 int *hint;
106 {
107     *hint = atoi(argv[0]);
108     return(UPCALL_STOP);
109 }
110
111
112 /* Used by qualified_get_server to make a list of servers to check */
113
114 qgetsv(argc, argv, sq)
115 int argc;
116 char **argv;
117 struct save_queue *sq;
118 {
119     sq_save_data(sq, strsave(argv[0]));
120     return(UPCALL_CONT);
121 }
122
123
124 /* Used by get_server_info to record all of the returned information */
125
126 getsvinfo(argc, argv, sserv)
127 int argc;
128 char **argv;
129 struct service *sserv;
130 {
131     sserv->service = strsave(argv[0]);
132     sserv->interval = atoi(argv[1]);
133     sserv->target = strsave(argv[2]);
134     sserv->script = strsave(argv[3]);
135     sserv->dfgen = atoi(argv[4]);
136     sserv->dfcheck = atoi(argv[5]);
137     sserv->type = strsave(argv[6]);
138     sserv->enable = atoi(argv[7]);
139     sserv->inprogress = atoi(argv[8]);
140     sserv->harderror = atoi(argv[9]);
141     sserv->errmsg = strsave(argv[10]);
142     return(UPCALL_STOP);
143 }
144
145
146 /* Scan the services and process any that need it. */
147
148 do_services()
149 {
150     char *qargv[6];
151     struct save_queue *sq, *sq_create();
152     char *service, dfgen_prog[64], dfgen_cmd[128];
153     struct service svc;
154     int status, lock_fd, ex;
155     struct timezone tz;
156     register char *p;
157     union wait waits;
158
159     if (dbg & DBG_VERBOSE)
160         com_err(whoami, 0, "starting pass over services");
161
162     qargv[0] = "true";
163     qargv[1] = "dontcare";
164     qargv[2] = "false";
165     sq = sq_create();
166     if (status = sms_query("qualified_get_server", 3, qargv, qgetsv, sq)) {
167         com_err(whoami, status, " getting services");
168         leave("query failed");
169     }
170     while (sq_get_data(sq, &service)) {
171         for (p = service; *p; p++)
172           if (isupper(*p))
173             *p = tolower(*p);
174         com_err(whoami, 0, "checking %s...", service);
175         qargv[0] = service;
176         sprintf(dfgen_prog, "%s/bin/%s.gen", SMS_DIR, service);
177         if (!file_exists(dfgen_prog)) {
178             com_err(whoami, 0, "prog %s doesn't exist\n", dfgen_prog);
179             free(service);
180             continue;
181         }
182         sprintf(dfgen_cmd, "exec %s %s/dcm/%s.out",
183                 dfgen_prog, SMS_DIR, service);
184         gettimeofday(&tv, &tz);
185         if (status = sms_query("get_server_info", 1, qargv, getsvinfo, &svc)) {
186             com_err(whoami, status, " getting service %s info", service);
187         }
188         svc.service = strsave(service);
189         qargv[0] = strsave(service);
190         qargv[1] = itoa(svc.dfgen);
191         qargv[2] = itoa(svc.dfcheck);
192         qargv[3] = strsave("0");
193         qargv[4] = itoa(svc.harderror);
194         qargv[5] = strsave(svc.errmsg);
195         if (svc.interval != 0) {
196             if (svc.interval * 60 + svc.dfcheck < tv.tv_sec) {
197                 lock_fd = maybe_lock_update(SMS_DIR, "@db@", service, 1);
198                 if (lock_fd < 0)
199                   goto free_service;
200                 free(qargv[3]);
201                 free(qargv[4]);
202                 free(qargv[5]);
203                 qargv[3] = strsave("1");
204                 qargv[4] = strsave("0");
205                 qargv[5] = strsave("");
206                 status = sms_query("set_server_internal_flags", 6, qargv,
207                                    scream, NULL);
208                 if (status != SMS_SUCCESS) {
209                     com_err(whoami, status, " setting server state");
210                     goto free_service;
211                 }
212             
213                 com_err(whoami, status, " running %s", dfgen_prog);
214                 waits.w_status = system(dfgen_cmd);
215                 /* extract the process's exit value */
216                 status = waits.w_retcode;
217                 if (status) {
218                     status += sms_err_base;
219                     com_err(whoami, status, " %s exited", dfgen_prog);
220                 }
221                 if (SOFT_FAIL(status)) {
222                     free(qargv[5]);
223                     qargv[5] = strsave(error_message(status));
224                 } else if (status == SMS_NO_CHANGE) {
225                     free(qargv[2]);
226                     qargv[2] = itoa(tv.tv_sec);
227                     svc.dfcheck = tv.tv_sec;
228                 } else if (status == SMS_SUCCESS) {
229                     free(qargv[1]);
230                     free(qargv[2]);
231                     qargv[1] = itoa(tv.tv_sec);
232                     qargv[2] = strsave(qargv[1]);
233                     svc.dfcheck = svc.dfgen = tv.tv_sec;
234                 } else { /* HARD_FAIL(status) */
235                     free(qargv[2]);
236                     free(qargv[4]);
237                     free(qargv[5]);
238                     qargv[2] = itoa(tv.tv_sec); 
239                     svc.dfcheck = tv.tv_sec;
240                     qargv[4] = itoa(status);
241                     qargv[5] = strsave(error_message(status));
242                     critical_alert("DCM","DCM building config files for %s: %s",
243                                   service, qargv[5]);
244                 }
245             free_service:
246                 free(qargv[3]);
247                 qargv[3] = strsave("0");
248                 status = sms_query("set_server_internal_flags", 6, qargv,
249                                    scream, NULL);
250                 close(lock_fd);
251                 free(qargv[0]);
252                 free(qargv[1]);
253                 free(qargv[2]);
254                 free(qargv[3]);
255                 free(qargv[4]);
256                 free(qargv[5]);
257             }
258             if (!strcmp(svc.type, "REPLICAT"))
259               ex = 1;
260             else
261               ex = 0;
262             lock_fd = maybe_lock_update(SMS_DIR, "@db@", service, ex);
263             if (lock_fd >= 0) {
264                 do_hosts(&svc);
265                 close(lock_fd);
266             }
267         }
268         free(svc.service);
269         free(svc.target);
270         free(svc.script);
271         free(svc.type);
272         free(svc.errmsg);
273         free(service);
274     }
275     sq_destroy(sq);
276 }
277
278
279 /* Used by qualified_get_server_host to make a list of hosts to check */
280
281 qgethost(argc, argv, sq)
282 int argc;
283 char **argv;
284 struct save_queue *sq;
285 {
286     sq_save_data(sq, strsave(argv[1]));
287     return(UPCALL_CONT);
288 }
289
290
291 /* Used by get_server_host_info to store all of the info about a host */
292
293 gethostinfo(argc, argv, shost)
294 int argc;
295 char **argv;
296 struct svrhost *shost;
297 {
298     shost->service = strsave(argv[0]);
299     shost->machine = strsave(argv[1]);
300     shost->enable = atoi(argv[2]);
301     shost->override = atoi(argv[3]);
302     shost->success = atoi(argv[4]);
303     shost->inprogress = atoi(argv[5]);
304     shost->hosterror = atoi(argv[6]);
305     shost->errmsg = strsave(argv[7]);
306     shost->lasttry = atoi(argv[8]);
307     shost->lastsuccess = atoi(argv[9]);
308     shost->value1 = atoi(argv[10]);
309     shost->value2 = atoi(argv[11]);
310     shost->value3 = strsave(argv[12]);
311     return(UPCALL_STOP);
312 }
313
314
315 /* Scans all of the hosts for a particular service, and processes them. */
316
317 do_hosts(svc)
318 struct service *svc;
319 {
320     char *argv[9], *machine;
321     int status, lock_fd;
322     struct save_queue *sq;
323     struct svrhost shost;
324
325     sq = sq_create();
326     argv[0] = svc->service;
327     argv[1] = "TRUE";
328     argv[2] = argv[3] = argv[4] = "DONTCARE";
329     argv[5] = "FALSE";
330     status = sms_query("qualified_get_server_host", 6, argv, qgethost, sq);
331     if (status == SMS_NO_MATCH) {
332         return;
333     } else if (status) {
334         com_err(whoami, status, " getting server_hosts for  %s", svc->service);
335         return;
336     }
337     while (sq_get_data(sq, &machine)) {
338         if (dbg & DBG_TRACE)
339           com_err(whoami, 0, "checking %s...", machine);
340         argv[1] = machine;
341         status = sms_query("get_server_host_info", 2, argv,gethostinfo, &shost);
342         if (status) {
343             com_err(whoami,status, " getting server_host_info for %s", machine);
344             goto free_mach;
345         }
346         if (!shost.enable || shost.hosterror ||
347             (shost.success && !shost.override &&
348              shost.lastsuccess > svc->dfgen)) {
349             if (dbg & DBG_TRACE)
350               com_err(whoami, 0, "not updating %s:%s", svc->service, machine);
351             goto free_mach;
352         }
353         if (!shost.success || shost.override ||
354             shost.lasttry + svc->interval < tv.tv_sec) {
355             lock_fd = maybe_lock_update(SMS_DIR, machine, svc->service, 1);
356             if (lock_fd < 0)
357               goto free_mach;
358             argv[0] = svc->service;
359             argv[1] = machine;
360             argv[2] = argv[3] = argv[5] = "0";
361             argv[4] = "1";
362             argv[6] = strsave("");
363             argv[7] = itoa(tv.tv_sec);
364             argv[8] = itoa(shost.lastsuccess);
365             status = sms_query("set_server_host_internal", 9, argv,scream,NULL);
366             if (status != SMS_SUCCESS) {
367                 com_err(whoami,status," while setting internal state for %s:%s",
368                         svc->service, machine);
369                 goto free_mach;
370             }
371             status = sms_update_server(svc->service, machine, svc->target,
372                                        svc->script);
373             if (status == SMS_SUCCESS) {
374                 argv[2] = "0";
375                 argv[3] = "1";
376                 free(argv[8]);
377                 argv[8] = itoa(tv.tv_sec);
378             } else if (SOFT_FAIL(status)) {
379                 free(argv[6]);
380                 argv[6] = strsave(error_message(status));
381             } else { /* HARD_FAIL */
382                 argv[2] = itoa(shost.override);
383                 argv[5] = itoa(status);
384                 free(argv[6]);
385                 argv[6] = strsave(error_message(status));
386                 critical_alert("DCM", "DCM updating %s:%s: %s",
387                                machine, svc->service, argv[6]);
388                 if (!strcmp(svc->type, "REPLICAT")) {
389                     char *qargv[6];
390
391                     svc->harderror = status;
392                     svc->errmsg = strsave(argv[6]);
393                     qargv[0] = strsave(svc->service);
394                     qargv[1] = itoa(svc->dfgen);
395                     qargv[2] = itoa(svc->dfcheck);
396                     qargv[3] = strsave("0");
397                     qargv[4] = itoa(svc->harderror);
398                     qargv[5] = strsave(svc->errmsg);
399                     status = sms_query("set_server_internal_flags",
400                                        6, qargv, scream, NULL);
401                     free(qargv[0]);
402                     free(qargv[1]);
403                     free(qargv[2]);
404                     free(qargv[3]);
405                     free(qargv[4]);
406                     free(qargv[5]);
407                     close(lock_fd);
408                     free(argv[2]);
409                     argv[4] = "0";
410                     free(argv[5]);
411                     status = sms_query("set_server_host_internal",
412                                        9, argv,scream,NULL);
413                     return(-1);
414                 }
415                 free(argv[2]);
416                 free(argv[5]);
417             }
418             argv[4] = "0";
419             close(lock_fd);
420             status = sms_query("set_server_host_internal", 9, argv,scream,NULL);
421         } else {
422             if (dbg & DBG_TRACE)
423               com_err(whoami, 0, "not updating %s", machine);
424         }
425     free_mach:
426         free(machine);
427         close(lock_fd);
428     }
429     return(0);
430 }
431
This page took 0.296531 seconds and 3 git commands to generate.