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 /* declared global so that we can get the current time from different places. */
51 dbg = atoi(getenv("DEBUG"));
57 while(++arg - argv < argc) {
61 dbg = atoi((*arg)[2]? *arg+2: *++arg);
65 set_com_err_hook(dcm_com_err_hook);
67 /* if /etc/nodcm exists, punt quietly. */
68 if (!access("/etc/nodcm", F_OK)) {
72 if (status = sms_connect("")) {
73 com_err(whoami, status, " on sms_connect");
74 leave("connect failed");
77 if (status = sms_auth("dcm")) {
78 com_err(whoami, status, " on \"authenticate\"");
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");
90 leave("dcm_enable not set");
100 /* Used by the get_value query when checking for dcm_enable. */
102 gqval(argc, argv, hint)
107 *hint = atoi(argv[0]);
112 /* Used by qualified_get_server to make a list of servers to check */
114 qgetsv(argc, argv, sq)
117 struct save_queue *sq;
119 sq_save_data(sq, strsave(argv[0]));
124 /* Used by get_server_info to record all of the returned information */
126 getsvinfo(argc, argv, sserv)
129 struct service *sserv;
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]);
146 /* Scan the services and process any that need it. */
151 struct save_queue *sq, *sq_create();
152 char *service, dfgen_prog[64], dfgen_cmd[128];
154 int status, lock_fd, ex;
159 if (dbg & DBG_VERBOSE)
160 com_err(whoami, 0, "starting pass over services");
163 qargv[1] = "dontcare";
166 if (status = sms_query("qualified_get_server", 3, qargv, qgetsv, sq)) {
167 com_err(whoami, status, " getting services");
168 leave("query failed");
170 while (sq_get_data(sq, &service)) {
171 for (p = service; *p; p++)
174 com_err(whoami, 0, "checking %s...", 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);
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, skipping to next service", service);
189 svc.service = strsave(service);
190 qargv[0] = strsave(service);
191 qargv[1] = itoa(svc.dfgen);
192 qargv[2] = itoa(svc.dfcheck);
193 qargv[3] = strsave("0");
194 qargv[4] = itoa(svc.harderror);
195 qargv[5] = strsave(svc.errmsg);
196 if (svc.interval != 0) {
197 if (svc.interval * 60 + svc.dfcheck < tv.tv_sec) {
198 lock_fd = maybe_lock_update(SMS_DIR, "@db@", service, 1);
204 qargv[3] = strsave("1");
205 qargv[4] = strsave("0");
206 qargv[5] = strsave("");
207 status = sms_query("set_server_internal_flags", 6, qargv,
209 if (status != SMS_SUCCESS) {
210 com_err(whoami, status, " setting server state");
214 com_err(whoami, status, " running %s", dfgen_prog);
215 waits.w_status = system(dfgen_cmd);
216 if (waits.w_termsig) {
217 status = SMS_TAR_FAIL;
218 com_err(whoami, status, " %s exited on signal %d",
219 dfgen_prog, waits.w_termsig);
220 } else if (waits.w_retcode) {
221 /* extract the process's exit value */
222 status = waits.w_retcode + sms_err_base;
223 com_err(whoami, status, " %s exited", dfgen_prog);
225 if (SOFT_FAIL(status)) {
227 qargv[5] = strsave(error_message(status));
228 } else if (status == SMS_NO_CHANGE) {
230 qargv[2] = itoa(tv.tv_sec);
231 svc.dfcheck = tv.tv_sec;
232 } else if (status == SMS_SUCCESS) {
235 qargv[1] = itoa(tv.tv_sec);
236 qargv[2] = strsave(qargv[1]);
237 svc.dfcheck = svc.dfgen = tv.tv_sec;
238 } else { /* HARD_FAIL(status) */
242 qargv[2] = itoa(tv.tv_sec);
243 svc.dfcheck = tv.tv_sec;
244 qargv[4] = itoa(status);
245 qargv[5] = strsave(error_message(status));
246 critical_alert("DCM","DCM building config files for %s: %s",
251 qargv[3] = strsave("0");
252 status = sms_query("set_server_internal_flags", 6, qargv,
255 com_err(whoami, status,
256 " setting service state, trying again");
257 status = sms_query("set_server_internal_flags", 6, qargv,
260 com_err(whoami, status, " setting service state again");
270 if (!strcmp(svc.type, "REPLICAT"))
274 lock_fd = maybe_lock_update(SMS_DIR, "@db@", service, ex);
291 /* Used by qualified_get_server_host to make a list of hosts to check */
293 qgethost(argc, argv, sq)
296 struct save_queue *sq;
298 sq_save_data(sq, strsave(argv[1]));
303 /* Used by get_server_host_info to store all of the info about a host */
305 gethostinfo(argc, argv, shost)
308 struct svrhost *shost;
310 shost->service = strsave(argv[0]);
311 shost->machine = strsave(argv[1]);
312 shost->enable = atoi(argv[2]);
313 shost->override = atoi(argv[3]);
314 shost->success = atoi(argv[4]);
315 shost->inprogress = atoi(argv[5]);
316 shost->hosterror = atoi(argv[6]);
317 shost->errmsg = strsave(argv[7]);
318 shost->lasttry = atoi(argv[8]);
319 shost->lastsuccess = atoi(argv[9]);
320 shost->value1 = atoi(argv[10]);
321 shost->value2 = atoi(argv[11]);
322 shost->value3 = strsave(argv[12]);
327 /* Scans all of the hosts for a particular service, and processes them. */
332 char *argv[9], *machine;
334 struct save_queue *sq;
335 struct svrhost shost;
338 argv[0] = svc->service;
340 argv[2] = argv[3] = argv[4] = "DONTCARE";
342 status = sms_query("qualified_get_server_host", 6, argv, qgethost, sq);
343 if (status == SMS_NO_MATCH) {
346 com_err(whoami, status, " getting server_hosts for %s", svc->service);
349 while (sq_get_data(sq, &machine)) {
351 com_err(whoami, 0, "checking %s...", machine);
353 status = sms_query("get_server_host_info", 2, argv,gethostinfo, &shost);
355 com_err(whoami,status, " getting server_host_info for %s", machine);
358 if (!shost.enable || shost.hosterror ||
359 (shost.success && !shost.override &&
360 shost.lastsuccess >= svc->dfgen)) {
362 com_err(whoami, 0, "not updating %s:%s", svc->service, machine);
366 lock_fd = maybe_lock_update(SMS_DIR, machine, svc->service, 1);
369 argv[0] = svc->service;
371 argv[2] = argv[3] = argv[5] = "0";
373 argv[6] = strsave("");
374 argv[7] = itoa(tv.tv_sec);
375 argv[8] = itoa(shost.lastsuccess);
376 status = sms_query("set_server_host_internal", 9, argv,scream,NULL);
377 if (status != SMS_SUCCESS) {
378 com_err(whoami,status," while setting internal state for %s:%s",
379 svc->service, machine);
382 status = sms_update_server(svc->service, machine, svc->target,
384 if (status == SMS_SUCCESS) {
388 argv[8] = itoa(tv.tv_sec);
389 } else if (SOFT_FAIL(status)) {
391 argv[6] = strsave(error_message(status));
392 } else { /* HARD_FAIL */
393 argv[2] = itoa(shost.override);
394 argv[5] = itoa(status);
396 argv[6] = strsave(error_message(status));
397 critical_alert("DCM", "DCM updating %s:%s: %s",
398 machine, svc->service, argv[6]);
399 if (!strcmp(svc->type, "REPLICAT")) {
402 svc->harderror = status;
403 svc->errmsg = strsave(argv[6]);
404 qargv[0] = strsave(svc->service);
405 qargv[1] = itoa(svc->dfgen);
406 qargv[2] = itoa(svc->dfcheck);
407 qargv[3] = strsave("0");
408 qargv[4] = itoa(svc->harderror);
409 qargv[5] = strsave(svc->errmsg);
410 status = sms_query("set_server_internal_flags",
411 6, qargv, scream, NULL);
413 com_err(whoami, status,
414 " setting service state, trying again");
415 status = sms_query("set_server_internal_flags",
416 6, qargv, scream, NULL);
418 com_err(whoami, status, " setting service state again");
430 status = sms_query("set_server_host_internal",
431 9, argv,scream,NULL);
433 com_err(whoami, status,
434 " setting host state, trying again");
435 status = sms_query("set_server_host_internal",
436 9, argv,scream,NULL);
438 com_err(whoami, status, " setting host state again");
447 status = sms_query("set_server_host_internal", 9, argv,scream,NULL);
449 com_err(whoami, status, " setting host state, trying again");
450 status = sms_query("set_server_host_internal", 9, argv,scream,NULL);
452 com_err(whoami, status, " setting host state again");