2 * The Data Control Manager for SMS.
4 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
5 * For copying and distribution information, see the file
14 static char rcsid_dcm_c[] = "$Header$";
26 #include "mit-copyright.h"
29 extern char *getenv();
31 extern char *error_message();
37 #define DEADLOCK_WAIT (3 * 60) /* number of seconds to wait after
38 a deadlock before trying again. */
40 /* declared global so that we can get the current time from different places. */
54 dbg = atoi(getenv("DEBUG"));
60 while(++arg - argv < argc) {
64 dbg = atoi((*arg)[2]? *arg+2: *++arg);
68 set_com_err_hook(dcm_com_err_hook);
70 /* if /etc/nodcm exists, punt quietly. */
71 if (!access("/etc/nodcm", F_OK)) {
75 if (status = sms_connect("")) {
76 com_err(whoami, status, " on sms_connect");
77 leave("connect failed");
80 if (status = sms_auth("dcm")) {
81 com_err(whoami, status, " on \"authenticate\"");
85 /* if DCM is not enabled, exit after logging */
86 qargv[0] = "dcm_enable";
87 if (status = sms_query("get_value", 1, qargv, gqval, &i)) {
88 com_err(whoami, status, " check dcm_enable");
89 leave("query failed");
93 leave("dcm_enable not set");
103 /* Used by the get_value query when checking for dcm_enable. */
105 gqval(argc, argv, hint)
110 *hint = atoi(argv[0]);
115 /* Used by qualified_get_server to make a list of servers to check */
117 qgetsv(argc, argv, sq)
120 struct save_queue *sq;
122 sq_save_data(sq, strsave(argv[0]));
127 /* Used by get_server_info to record all of the returned information */
129 getsvinfo(argc, argv, sserv)
132 struct service *sserv;
134 sserv->service = strsave(argv[0]);
135 sserv->interval = atoi(argv[1]);
136 sserv->target = strsave(argv[2]);
137 sserv->script = strsave(argv[3]);
138 sserv->dfgen = atoi(argv[4]);
139 sserv->dfcheck = atoi(argv[5]);
140 sserv->type = strsave(argv[6]);
141 sserv->enable = atoi(argv[7]);
142 sserv->inprogress = atoi(argv[8]);
143 sserv->harderror = atoi(argv[9]);
144 sserv->errmsg = strsave(argv[10]);
149 /* Scan the services and process any that need it. */
154 struct save_queue *sq, *sq_create();
155 char *service, dfgen_prog[64], dfgen_cmd[128];
157 int status, lock_fd, ex;
162 if (dbg & DBG_VERBOSE)
163 com_err(whoami, 0, "starting pass over services");
166 qargv[1] = "dontcare";
169 if (status = sms_query("qualified_get_server", 3, qargv, qgetsv, sq)) {
170 com_err(whoami, status, " getting services");
171 leave("query failed");
173 while (sq_get_data(sq, &service)) {
174 for (p = service; *p; p++)
177 com_err(whoami, 0, "checking %s...", service);
179 sprintf(dfgen_prog, "%s/bin/%s.gen", SMS_DIR, service);
180 if (!file_exists(dfgen_prog)) {
181 com_err(whoami, 0, "prog %s doesn't exist\n", dfgen_prog);
185 sprintf(dfgen_cmd, "exec %s %s/dcm/%s.out",
186 dfgen_prog, SMS_DIR, service);
187 gettimeofday(&tv, &tz);
188 if (status = sms_query("get_server_info", 1, qargv, getsvinfo, &svc)) {
189 com_err(whoami, status, " getting service %s info, skipping to next service", service);
192 svc.service = strsave(service);
193 qargv[0] = strsave(service);
194 qargv[1] = itoa(svc.dfgen);
195 qargv[2] = itoa(svc.dfcheck);
196 qargv[3] = strsave("0");
197 qargv[4] = itoa(svc.harderror);
198 qargv[5] = strsave(svc.errmsg);
199 if (svc.interval != 0) {
200 if (svc.interval * 60 + svc.dfcheck < tv.tv_sec) {
201 lock_fd = maybe_lock_update(SMS_DIR, "@db@", service, 1);
207 qargv[3] = strsave("1");
208 qargv[4] = strsave("0");
209 qargv[5] = strsave("");
210 status = sms_query("set_server_internal_flags", 6, qargv,
212 if (status != SMS_SUCCESS) {
213 com_err(whoami, status, " setting server state");
217 com_err(whoami, status, " running %s", dfgen_prog);
218 waits.w_status = system(dfgen_cmd);
219 if (waits.w_termsig) {
220 status = SMS_TAR_FAIL;
221 com_err(whoami, status, " %s exited on signal %d",
222 dfgen_prog, waits.w_termsig);
223 } else if (waits.w_retcode) {
224 /* extract the process's exit value */
225 status = waits.w_retcode + sms_err_base;
226 com_err(whoami, status, " %s exited", dfgen_prog);
228 if (SOFT_FAIL(status)) {
230 qargv[5] = strsave(error_message(status));
231 } else if (status == SMS_NO_CHANGE) {
233 qargv[2] = itoa(tv.tv_sec);
234 svc.dfcheck = tv.tv_sec;
235 } else if (status == SMS_SUCCESS) {
238 qargv[1] = itoa(tv.tv_sec);
239 qargv[2] = strsave(qargv[1]);
240 svc.dfcheck = svc.dfgen = tv.tv_sec;
241 } else { /* HARD_FAIL(status) */
245 qargv[2] = itoa(tv.tv_sec);
246 svc.dfcheck = tv.tv_sec;
247 qargv[4] = itoa(status);
248 qargv[5] = strsave(error_message(status));
249 critical_alert("DCM","DCM building config files for %s: %s",
254 qargv[3] = strsave("0");
255 status = sms_query("set_server_internal_flags", 6, qargv,
258 com_err(whoami, status,
259 " setting service state, sleeping");
260 sleep(DEADLOCK_WAIT);
261 status = sms_query("set_server_internal_flags", 6, qargv,
264 com_err(whoami, status, " setting service state again");
274 if (!strcmp(svc.type, "REPLICAT"))
278 lock_fd = maybe_lock_update(SMS_DIR, "@db@", service, ex);
295 /* Used by qualified_get_server_host to make a list of hosts to check */
297 qgethost(argc, argv, sq)
300 struct save_queue *sq;
302 sq_save_data(sq, strsave(argv[1]));
307 /* Used by get_server_host_info to store all of the info about a host */
309 gethostinfo(argc, argv, shost)
312 struct svrhost *shost;
314 shost->service = strsave(argv[0]);
315 shost->machine = strsave(argv[1]);
316 shost->enable = atoi(argv[2]);
317 shost->override = atoi(argv[3]);
318 shost->success = atoi(argv[4]);
319 shost->inprogress = atoi(argv[5]);
320 shost->hosterror = atoi(argv[6]);
321 shost->errmsg = strsave(argv[7]);
322 shost->lasttry = atoi(argv[8]);
323 shost->lastsuccess = atoi(argv[9]);
324 shost->value1 = atoi(argv[10]);
325 shost->value2 = atoi(argv[11]);
326 shost->value3 = strsave(argv[12]);
331 /* Scans all of the hosts for a particular service, and processes them. */
336 char *argv[9], *machine;
338 struct save_queue *sq;
339 struct svrhost shost;
342 argv[0] = svc->service;
344 argv[2] = argv[3] = argv[4] = "DONTCARE";
346 status = sms_query("qualified_get_server_host", 6, argv, qgethost, sq);
347 if (status == SMS_NO_MATCH) {
350 com_err(whoami, status, " getting server_hosts for %s", svc->service);
353 while (sq_get_data(sq, &machine)) {
355 com_err(whoami, 0, "checking %s...", machine);
357 status = sms_query("get_server_host_info", 2, argv,gethostinfo, &shost);
359 com_err(whoami,status, " getting server_host_info for %s", machine);
362 if (!shost.enable || shost.hosterror ||
363 (shost.success && !shost.override &&
364 shost.lastsuccess >= svc->dfgen)) {
366 com_err(whoami, 0, "not updating %s:%s", svc->service, machine);
370 lock_fd = maybe_lock_update(SMS_DIR, machine, svc->service, 1);
373 argv[0] = svc->service;
375 argv[2] = argv[3] = argv[5] = "0";
377 argv[6] = strsave("");
378 argv[7] = itoa(tv.tv_sec);
379 argv[8] = itoa(shost.lastsuccess);
380 status = sms_query("set_server_host_internal", 9, argv,scream,NULL);
381 if (status != SMS_SUCCESS) {
382 com_err(whoami,status," while setting internal state for %s:%s",
383 svc->service, machine);
386 status = sms_update_server(svc->service, machine, svc->target,
388 if (status == SMS_SUCCESS) {
392 argv[8] = itoa(tv.tv_sec);
393 } else if (SOFT_FAIL(status)) {
395 argv[6] = strsave(error_message(status));
396 } else { /* HARD_FAIL */
397 argv[2] = itoa(shost.override);
398 argv[5] = itoa(status);
400 argv[6] = strsave(error_message(status));
401 critical_alert("DCM", "DCM updating %s:%s: %s",
402 machine, svc->service, argv[6]);
403 if (!strcmp(svc->type, "REPLICAT")) {
406 svc->harderror = status;
407 svc->errmsg = strsave(argv[6]);
408 qargv[0] = strsave(svc->service);
409 qargv[1] = itoa(svc->dfgen);
410 qargv[2] = itoa(svc->dfcheck);
411 qargv[3] = strsave("0");
412 qargv[4] = itoa(svc->harderror);
413 qargv[5] = strsave(svc->errmsg);
414 status = sms_query("set_server_internal_flags",
415 6, qargv, scream, NULL);
417 com_err(whoami, status,
418 " setting service state, sleeping");
419 sleep(DEADLOCK_WAIT);
420 status = sms_query("set_server_internal_flags",
421 6, qargv, scream, NULL);
423 com_err(whoami, status, " setting service state again");
435 status = sms_query("set_server_host_internal",
436 9, argv,scream,NULL);
438 com_err(whoami, status,
439 " setting host state, sleeping");
440 sleep(DEADLOCK_WAIT);
441 status = sms_query("set_server_host_internal",
442 9, argv,scream,NULL);
444 com_err(whoami, status, " setting host state again");
453 status = sms_query("set_server_host_internal", 9, argv,scream,NULL);
455 com_err(whoami, status, " setting host state, sleeping");
456 sleep(DEADLOCK_WAIT);
457 status = sms_query("set_server_host_internal", 9, argv,scream,NULL);
459 com_err(whoami, status, " setting host state again");