watchquagga.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. /*
  2. Monitor status of quagga daemons and restart if necessary.
  3. Copyright (C) 2004 Andrew J. Schorr
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <zebra.h>
  17. #include <thread.h>
  18. #include <log.h>
  19. #include <network.h>
  20. #include <sigevent.h>
  21. #include <lib/version.h>
  22. #include <getopt.h>
  23. #include <sys/un.h>
  24. #include <sys/wait.h>
  25. #include <memory.h>
  26. #ifndef MIN
  27. #define MIN(X,Y) (((X) <= (Y)) ? (X) : (Y))
  28. #endif
  29. /* Macros to help randomize timers. */
  30. #define JITTER(X) ((random() % ((X)+1))-((X)/2))
  31. #define FUZZY(X) ((X)+JITTER((X)/20))
  32. #define DEFAULT_PERIOD 5
  33. #define DEFAULT_TIMEOUT 10
  34. #define DEFAULT_RESTART_TIMEOUT 20
  35. #define DEFAULT_LOGLEVEL LOG_INFO
  36. #define DEFAULT_MIN_RESTART 60
  37. #define DEFAULT_MAX_RESTART 600
  38. #ifdef PATH_WATCHQUAGGA_PID
  39. #define DEFAULT_PIDFILE PATH_WATCHQUAGGA_PID
  40. #else
  41. #define DEFAULT_PIDFILE STATEDIR "/watchquagga.pid"
  42. #endif
  43. #ifdef DAEMON_VTY_DIR
  44. #define VTYDIR DAEMON_VTY_DIR
  45. #else
  46. #define VTYDIR STATEDIR
  47. #endif
  48. #define PING_TOKEN "PING"
  49. /* Needs to be global, referenced somewhere inside libzebra. */
  50. struct thread_master *master;
  51. typedef enum
  52. {
  53. MODE_MONITOR = 0,
  54. MODE_GLOBAL_RESTART,
  55. MODE_SEPARATE_RESTART,
  56. MODE_PHASED_ZEBRA_RESTART,
  57. MODE_PHASED_ALL_RESTART
  58. } watch_mode_t;
  59. static const char *mode_str[] =
  60. {
  61. "monitor",
  62. "global restart",
  63. "individual daemon restart",
  64. "phased zebra restart",
  65. "phased global restart for any failure",
  66. };
  67. typedef enum
  68. {
  69. PHASE_NONE = 0,
  70. PHASE_STOPS_PENDING,
  71. PHASE_WAITING_DOWN,
  72. PHASE_ZEBRA_RESTART_PENDING,
  73. PHASE_WAITING_ZEBRA_UP
  74. } restart_phase_t;
  75. static const char *phase_str[] =
  76. {
  77. "None",
  78. "Stop jobs running",
  79. "Waiting for other daemons to come down",
  80. "Zebra restart job running",
  81. "Waiting for zebra to come up",
  82. "Start jobs running",
  83. };
  84. #define PHASE_TIMEOUT (3*gs.restart_timeout)
  85. struct restart_info
  86. {
  87. const char *name;
  88. const char *what;
  89. pid_t pid;
  90. struct timeval time;
  91. long interval;
  92. struct thread *t_kill;
  93. int kills;
  94. };
  95. static struct global_state
  96. {
  97. watch_mode_t mode;
  98. restart_phase_t phase;
  99. struct thread *t_phase_hanging;
  100. const char *vtydir;
  101. long period;
  102. long timeout;
  103. long restart_timeout;
  104. long min_restart_interval;
  105. long max_restart_interval;
  106. int do_ping;
  107. struct daemon *daemons;
  108. const char *restart_command;
  109. const char *start_command;
  110. const char *stop_command;
  111. struct restart_info restart;
  112. int unresponsive_restart;
  113. int loglevel;
  114. struct daemon *special; /* points to zebra when doing phased restart */
  115. int numdaemons;
  116. int numpids;
  117. int numdown; /* # of daemons that are not UP or UNRESPONSIVE */
  118. } gs = {
  119. .mode = MODE_MONITOR,
  120. .phase = PHASE_NONE,
  121. .vtydir = VTYDIR,
  122. .period = 1000*DEFAULT_PERIOD,
  123. .timeout = DEFAULT_TIMEOUT,
  124. .restart_timeout = DEFAULT_RESTART_TIMEOUT,
  125. .loglevel = DEFAULT_LOGLEVEL,
  126. .min_restart_interval = DEFAULT_MIN_RESTART,
  127. .max_restart_interval = DEFAULT_MAX_RESTART,
  128. .do_ping = 1,
  129. };
  130. typedef enum
  131. {
  132. DAEMON_INIT,
  133. DAEMON_DOWN,
  134. DAEMON_CONNECTING,
  135. DAEMON_UP,
  136. DAEMON_UNRESPONSIVE
  137. } daemon_state_t;
  138. #define IS_UP(DMN) \
  139. (((DMN)->state == DAEMON_UP) || ((DMN)->state == DAEMON_UNRESPONSIVE))
  140. static const char *state_str[] =
  141. {
  142. "Init",
  143. "Down",
  144. "Connecting",
  145. "Up",
  146. "Unresponsive",
  147. };
  148. struct daemon {
  149. const char *name;
  150. daemon_state_t state;
  151. int fd;
  152. struct timeval echo_sent;
  153. u_int connect_tries;
  154. struct thread *t_wakeup;
  155. struct thread *t_read;
  156. struct thread *t_write;
  157. struct daemon *next;
  158. struct restart_info restart;
  159. };
  160. static const struct option longopts[] =
  161. {
  162. { "daemon", no_argument, NULL, 'd'},
  163. { "statedir", required_argument, NULL, 'S'},
  164. { "no-echo", no_argument, NULL, 'e'},
  165. { "loglevel", required_argument, NULL, 'l'},
  166. { "interval", required_argument, NULL, 'i'},
  167. { "timeout", required_argument, NULL, 't'},
  168. { "restart-timeout", required_argument, NULL, 'T'},
  169. { "restart", required_argument, NULL, 'r'},
  170. { "start-command", required_argument, NULL, 's'},
  171. { "kill-command", required_argument, NULL, 'k'},
  172. { "restart-all", required_argument, NULL, 'R'},
  173. { "all-restart", no_argument, NULL, 'a'},
  174. { "always-all-restart", no_argument, NULL, 'A'},
  175. { "unresponsive-restart", no_argument, NULL, 'z'},
  176. { "min-restart-interval", required_argument, NULL, 'm'},
  177. { "max-restart-interval", required_argument, NULL, 'M'},
  178. { "pid-file", required_argument, NULL, 'p'},
  179. { "blank-string", required_argument, NULL, 'b'},
  180. { "help", no_argument, NULL, 'h'},
  181. { "version", no_argument, NULL, 'v'},
  182. { NULL, 0, NULL, 0 }
  183. };
  184. static int try_connect(struct daemon *dmn);
  185. static int wakeup_send_echo(struct thread *t_wakeup);
  186. static void try_restart(struct daemon *dmn);
  187. static void phase_check(void);
  188. static int
  189. usage(const char *progname, int status)
  190. {
  191. if (status != 0)
  192. fprintf(stderr, "Try `%s --help' for more information.\n", progname);
  193. else
  194. {
  195. printf("Usage : %s [OPTION...] <daemon name> ...\n\n\
  196. Watchdog program to monitor status of quagga daemons and try to restart\n\
  197. them if they are down or unresponsive. It determines whether a daemon is\n\
  198. up based on whether it can connect to the daemon's vty unix stream socket.\n\
  199. It then repeatedly sends echo commands over that socket to determine whether\n\
  200. the daemon is responsive. If the daemon crashes, we will receive an EOF\n\
  201. on the socket connection and know immediately that the daemon is down.\n\n\
  202. The daemons to be monitored should be listed on the command line.\n\n\
  203. This program can run in one of 5 modes:\n\n\
  204. 0. Mode: %s.\n\
  205. Just monitor and report on status changes. Example:\n\
  206. %s -d zebra ospfd bgpd\n\n\
  207. 1. Mode: %s.\n\
  208. Whenever any daemon hangs or crashes, use the given command to restart\n\
  209. them all. Example:\n\
  210. %s -dz \\\n\
  211. -R '/sbin/service zebra restart; /sbin/service ospfd restart' \\\n\
  212. zebra ospfd\n\n\
  213. 2. Mode: %s.\n\
  214. When any single daemon hangs or crashes, restart only the daemon that's\n\
  215. in trouble using the supplied restart command. Example:\n\
  216. %s -dz -r '/sbin/service %%s restart' zebra ospfd bgpd\n\n\
  217. 3. Mode: %s.\n\
  218. The same as the previous mode, except that there is special treatment when\n\
  219. the zebra daemon is in trouble. In that case, a phased restart approach\n\
  220. is used: 1. stop all other daemons; 2. restart zebra; 3. start the other\n\
  221. daemons. Example:\n\
  222. %s -adz -r '/sbin/service %%s restart' \\\n\
  223. -s '/sbin/service %%s start' \\\n\
  224. -k '/sbin/service %%s stop' zebra ospfd bgpd\n\n\
  225. 4. Mode: %s.\n\
  226. This is the same as the previous mode, except that the phased restart\n\
  227. procedure is used whenever any of the daemons hangs or crashes. Example:\n\
  228. %s -Adz -r '/sbin/service %%s restart' \\\n\
  229. -s '/sbin/service %%s start' \\\n\
  230. -k '/sbin/service %%s stop' zebra ospfd bgpd\n\n\
  231. As of this writing, it is believed that mode 2 [%s]\n\
  232. is not safe, and mode 3 [%s] may not be safe with some of the\n\
  233. routing daemons.\n\n\
  234. In order to avoid attempting to restart the daemons in a fast loop,\n\
  235. the -m and -M options allow you to control the minimum delay between\n\
  236. restart commands. The minimum restart delay is recalculated each time\n\
  237. a restart is attempted: if the time since the last restart attempt exceeds\n\
  238. twice the -M value, then the restart delay is set to the -m value.\n\
  239. Otherwise, the interval is doubled (but capped at the -M value).\n\n",
  240. progname,mode_str[0],progname,mode_str[1],progname,mode_str[2],
  241. progname,mode_str[3],progname,mode_str[4],progname,mode_str[2],
  242. mode_str[3]);
  243. printf("Options:\n\
  244. -d, --daemon Run in daemon mode. In this mode, error messages are sent\n\
  245. to syslog instead of stdout.\n\
  246. -S, --statedir Set the vty socket directory (default is %s)\n\
  247. -e, --no-echo Do not ping the daemons to test responsiveness (this\n\
  248. option is necessary if the daemons do not support the\n\
  249. echo command)\n\
  250. -l, --loglevel Set the logging level (default is %d).\n\
  251. The value should range from %d (LOG_EMERG) to %d (LOG_DEBUG),\n\
  252. but it can be set higher than %d if extra-verbose debugging\n\
  253. messages are desired.\n\
  254. -m, --min-restart-interval\n\
  255. Set the minimum seconds to wait between invocations of daemon\n\
  256. restart commands (default is %d).\n\
  257. -M, --max-restart-interval\n\
  258. Set the maximum seconds to wait between invocations of daemon\n\
  259. restart commands (default is %d).\n\
  260. -i, --interval Set the status polling interval in seconds (default is %d)\n\
  261. -t, --timeout Set the unresponsiveness timeout in seconds (default is %d)\n\
  262. -T, --restart-timeout\n\
  263. Set the restart (kill) timeout in seconds (default is %d).\n\
  264. If any background jobs are still running after this much\n\
  265. time has elapsed, they will be killed.\n\
  266. -r, --restart Supply a Bourne shell command to use to restart a single\n\
  267. daemon. The command string should include '%%s' where the\n\
  268. name of the daemon should be substituted.\n\
  269. Note that -r and -R are incompatible.\n\
  270. -s, --start-command\n\
  271. Supply a Bourne shell to command to use to start a single\n\
  272. daemon. The command string should include '%%s' where the\n\
  273. name of the daemon should be substituted.\n\
  274. -k, --kill-command\n\
  275. Supply a Bourne shell to command to use to stop a single\n\
  276. daemon. The command string should include '%%s' where the\n\
  277. name of the daemon should be substituted.\n\
  278. -R, --restart-all\n\
  279. When one or more daemons is down, try to restart everything\n\
  280. using the Bourne shell command supplied as the argument.\n\
  281. Note that -r and -R are incompatible.\n\
  282. -z, --unresponsive-restart\n\
  283. When a daemon is unresponsive, treat it as being down for\n\
  284. restart purposes.\n\
  285. -a, --all-restart\n\
  286. When zebra hangs or crashes, restart all daemons using\n\
  287. this phased approach: 1. stop all other daemons; 2. restart\n\
  288. zebra; 3. start other daemons. Requires -r, -s, and -k.\n\
  289. -A, --always-all-restart\n\
  290. When any daemon (not just zebra) hangs or crashes, use the\n\
  291. same phased restart mechanism described above for -a.\n\
  292. Requires -r, -s, and -k.\n\
  293. -p, --pid-file Set process identifier file name\n\
  294. (default is %s).\n\
  295. -b, --blank-string\n\
  296. When the supplied argument string is found in any of the\n\
  297. various shell command arguments (-r, -s, -k, or -R), replace\n\
  298. it with a space. This is an ugly hack to circumvent problems\n\
  299. passing command-line arguments with embedded spaces.\n\
  300. -v, --version Print program version\n\
  301. -h, --help Display this help and exit\n",
  302. VTYDIR,DEFAULT_LOGLEVEL,LOG_EMERG,LOG_DEBUG,LOG_DEBUG,
  303. DEFAULT_MIN_RESTART,DEFAULT_MAX_RESTART,
  304. DEFAULT_PERIOD,DEFAULT_TIMEOUT,DEFAULT_RESTART_TIMEOUT,
  305. DEFAULT_PIDFILE);
  306. }
  307. return status;
  308. }
  309. static pid_t
  310. run_background(char *shell_cmd)
  311. {
  312. pid_t child;
  313. switch (child = fork())
  314. {
  315. case -1:
  316. zlog_err("fork failed, cannot run command [%s]: %s",
  317. shell_cmd,safe_strerror(errno));
  318. return -1;
  319. case 0:
  320. /* Child process. */
  321. /* Use separate process group so child processes can be killed easily. */
  322. if (setpgid(0,0) < 0)
  323. zlog_warn("warning: setpgid(0,0) failed: %s",safe_strerror(errno));
  324. {
  325. char shell[] = "sh";
  326. char dashc[] = "-c";
  327. char *const argv[4] = { shell, dashc, shell_cmd, NULL};
  328. execv("/bin/sh", argv);
  329. zlog_err("execv(/bin/sh -c '%s') failed: %s",
  330. shell_cmd,safe_strerror(errno));
  331. _exit(127);
  332. }
  333. default:
  334. /* Parent process: we will reap the child later. */
  335. zlog_err("Forked background command [pid %d]: %s",(int)child,shell_cmd);
  336. return child;
  337. }
  338. }
  339. static struct timeval *
  340. time_elapsed(struct timeval *result, const struct timeval *start_time)
  341. {
  342. gettimeofday(result,NULL);
  343. result->tv_sec -= start_time->tv_sec;
  344. result->tv_usec -= start_time->tv_usec;
  345. while (result->tv_usec < 0)
  346. {
  347. result->tv_usec += 1000000L;
  348. result->tv_sec--;
  349. }
  350. return result;
  351. }
  352. static int
  353. restart_kill(struct thread *t_kill)
  354. {
  355. struct restart_info *restart = THREAD_ARG(t_kill);
  356. struct timeval delay;
  357. time_elapsed(&delay,&restart->time);
  358. zlog_warn("Warning: %s %s child process %d still running after "
  359. "%ld seconds, sending signal %d",
  360. restart->what,restart->name,(int)restart->pid, (long)delay.tv_sec,
  361. (restart->kills ? SIGKILL : SIGTERM));
  362. kill(-restart->pid,(restart->kills ? SIGKILL : SIGTERM));
  363. restart->kills++;
  364. restart->t_kill = thread_add_timer(master,restart_kill,restart,
  365. gs.restart_timeout);
  366. return 0;
  367. }
  368. static struct restart_info *
  369. find_child(pid_t child)
  370. {
  371. if (gs.mode == MODE_GLOBAL_RESTART)
  372. {
  373. if (gs.restart.pid == child)
  374. return &gs.restart;
  375. }
  376. else
  377. {
  378. struct daemon *dmn;
  379. for (dmn = gs.daemons; dmn; dmn = dmn->next)
  380. {
  381. if (dmn->restart.pid == child)
  382. return &dmn->restart;
  383. }
  384. }
  385. return NULL;
  386. }
  387. static void
  388. sigchild(void)
  389. {
  390. pid_t child;
  391. int status;
  392. const char *name;
  393. const char *what;
  394. struct restart_info *restart;
  395. switch (child = waitpid(-1,&status,WNOHANG))
  396. {
  397. case -1:
  398. zlog_err("waitpid failed: %s",safe_strerror(errno));
  399. return;
  400. case 0:
  401. zlog_warn("SIGCHLD received, but waitpid did not reap a child");
  402. return;
  403. }
  404. if ((restart = find_child(child)) != NULL)
  405. {
  406. name = restart->name;
  407. what = restart->what;
  408. restart->pid = 0;
  409. gs.numpids--;
  410. thread_cancel(restart->t_kill);
  411. restart->t_kill = NULL;
  412. /* Update restart time to reflect the time the command completed. */
  413. gettimeofday(&restart->time,NULL);
  414. }
  415. else
  416. {
  417. zlog_err("waitpid returned status for an unknown child process %d",
  418. (int)child);
  419. name = "(unknown)";
  420. what = "background";
  421. }
  422. if (WIFSTOPPED(status))
  423. zlog_warn("warning: %s %s process %d is stopped",
  424. what,name,(int)child);
  425. else if (WIFSIGNALED(status))
  426. zlog_warn("%s %s process %d terminated due to signal %d",
  427. what,name,(int)child,WTERMSIG(status));
  428. else if (WIFEXITED(status))
  429. {
  430. if (WEXITSTATUS(status) != 0)
  431. zlog_warn("%s %s process %d exited with non-zero status %d",
  432. what,name,(int)child,WEXITSTATUS(status));
  433. else
  434. zlog_debug("%s %s process %d exited normally",what,name,(int)child);
  435. }
  436. else
  437. zlog_err("cannot interpret %s %s process %d wait status 0x%x",
  438. what,name,(int)child,status);
  439. phase_check();
  440. }
  441. static int
  442. run_job(struct restart_info *restart, const char *cmdtype, const char *command,
  443. int force, int update_interval)
  444. {
  445. struct timeval delay;
  446. if (gs.loglevel > LOG_DEBUG+1)
  447. zlog_debug("attempting to %s %s",cmdtype,restart->name);
  448. if (restart->pid)
  449. {
  450. if (gs.loglevel > LOG_DEBUG+1)
  451. zlog_debug("cannot %s %s, previous pid %d still running",
  452. cmdtype,restart->name,(int)restart->pid);
  453. return -1;
  454. }
  455. /* Note: time_elapsed test must come before the force test, since we need
  456. to make sure that delay is initialized for use below in updating the
  457. restart interval. */
  458. if ((time_elapsed(&delay,&restart->time)->tv_sec < restart->interval) &&
  459. !force)
  460. {
  461. if (gs.loglevel > LOG_DEBUG+1)
  462. zlog_debug("postponing %s %s: "
  463. "elapsed time %ld < retry interval %ld",
  464. cmdtype,restart->name,(long)delay.tv_sec,restart->interval);
  465. return -1;
  466. }
  467. gettimeofday(&restart->time,NULL);
  468. restart->kills = 0;
  469. {
  470. char cmd[strlen(command)+strlen(restart->name)+1];
  471. snprintf(cmd,sizeof(cmd),command,restart->name);
  472. if ((restart->pid = run_background(cmd)) > 0)
  473. {
  474. restart->t_kill = thread_add_timer(master,restart_kill,restart,
  475. gs.restart_timeout);
  476. restart->what = cmdtype;
  477. gs.numpids++;
  478. }
  479. else
  480. restart->pid = 0;
  481. }
  482. /* Calculate the new restart interval. */
  483. if (update_interval)
  484. {
  485. if (delay.tv_sec > 2*gs.max_restart_interval)
  486. restart->interval = gs.min_restart_interval;
  487. else if ((restart->interval *= 2) > gs.max_restart_interval)
  488. restart->interval = gs.max_restart_interval;
  489. if (gs.loglevel > LOG_DEBUG+1)
  490. zlog_debug("restart %s interval is now %ld",
  491. restart->name,restart->interval);
  492. }
  493. return restart->pid;
  494. }
  495. #define SET_READ_HANDLER(DMN) \
  496. (DMN)->t_read = thread_add_read(master,handle_read,(DMN),(DMN)->fd)
  497. #define SET_WAKEUP_DOWN(DMN) \
  498. (DMN)->t_wakeup = thread_add_timer_msec(master,wakeup_down,(DMN), \
  499. FUZZY(gs.period))
  500. #define SET_WAKEUP_UNRESPONSIVE(DMN) \
  501. (DMN)->t_wakeup = thread_add_timer_msec(master,wakeup_unresponsive,(DMN), \
  502. FUZZY(gs.period))
  503. #define SET_WAKEUP_ECHO(DMN) \
  504. (DMN)->t_wakeup = thread_add_timer_msec(master,wakeup_send_echo,(DMN), \
  505. FUZZY(gs.period))
  506. static int
  507. wakeup_down(struct thread *t_wakeup)
  508. {
  509. struct daemon *dmn = THREAD_ARG(t_wakeup);
  510. dmn->t_wakeup = NULL;
  511. if (try_connect(dmn) < 0)
  512. SET_WAKEUP_DOWN(dmn);
  513. if ((dmn->connect_tries > 1) && (dmn->state != DAEMON_UP))
  514. try_restart(dmn);
  515. return 0;
  516. }
  517. static int
  518. wakeup_init(struct thread *t_wakeup)
  519. {
  520. struct daemon *dmn = THREAD_ARG(t_wakeup);
  521. dmn->t_wakeup = NULL;
  522. if (try_connect(dmn) < 0)
  523. {
  524. SET_WAKEUP_DOWN(dmn);
  525. zlog_err("%s state -> down : initial connection attempt failed",
  526. dmn->name);
  527. dmn->state = DAEMON_DOWN;
  528. }
  529. return 0;
  530. }
  531. static void
  532. daemon_down(struct daemon *dmn, const char *why)
  533. {
  534. if (IS_UP(dmn) || (dmn->state == DAEMON_INIT))
  535. zlog_err("%s state -> down : %s",dmn->name,why);
  536. else if (gs.loglevel > LOG_DEBUG)
  537. zlog_debug("%s still down : %s",dmn->name,why);
  538. if (IS_UP(dmn))
  539. gs.numdown++;
  540. dmn->state = DAEMON_DOWN;
  541. if (dmn->fd >= 0)
  542. {
  543. close(dmn->fd);
  544. dmn->fd = -1;
  545. }
  546. THREAD_OFF(dmn->t_read);
  547. THREAD_OFF(dmn->t_write);
  548. THREAD_OFF(dmn->t_wakeup);
  549. if (try_connect(dmn) < 0)
  550. SET_WAKEUP_DOWN(dmn);
  551. phase_check();
  552. }
  553. static int
  554. handle_read(struct thread *t_read)
  555. {
  556. struct daemon *dmn = THREAD_ARG(t_read);
  557. static const char resp[sizeof(PING_TOKEN)+4] = PING_TOKEN "\n";
  558. char buf[sizeof(resp)+100];
  559. ssize_t rc;
  560. struct timeval delay;
  561. dmn->t_read = NULL;
  562. if ((rc = read(dmn->fd,buf,sizeof(buf))) < 0)
  563. {
  564. char why[100];
  565. if (ERRNO_IO_RETRY(errno))
  566. {
  567. /* Pretend it never happened. */
  568. SET_READ_HANDLER(dmn);
  569. return 0;
  570. }
  571. snprintf(why,sizeof(why),"unexpected read error: %s",
  572. safe_strerror(errno));
  573. daemon_down(dmn,why);
  574. return 0;
  575. }
  576. if (rc == 0)
  577. {
  578. daemon_down(dmn,"read returned EOF");
  579. return 0;
  580. }
  581. if (!dmn->echo_sent.tv_sec)
  582. {
  583. char why[sizeof(buf)+100];
  584. snprintf(why,sizeof(why),"unexpected read returns %d bytes: %.*s",
  585. (int)rc,(int)rc,buf);
  586. daemon_down(dmn,why);
  587. return 0;
  588. }
  589. /* We are expecting an echo response: is there any chance that the
  590. response would not be returned entirely in the first read? That
  591. seems inconceivable... */
  592. if ((rc != sizeof(resp)) || memcmp(buf,resp,sizeof(resp)))
  593. {
  594. char why[100+sizeof(buf)];
  595. snprintf(why,sizeof(why),"read returned bad echo response of %d bytes "
  596. "(expecting %u): %.*s",
  597. (int)rc,(u_int)sizeof(resp),(int)rc,buf);
  598. daemon_down(dmn,why);
  599. return 0;
  600. }
  601. time_elapsed(&delay,&dmn->echo_sent);
  602. dmn->echo_sent.tv_sec = 0;
  603. if (dmn->state == DAEMON_UNRESPONSIVE)
  604. {
  605. if (delay.tv_sec < gs.timeout)
  606. {
  607. dmn->state = DAEMON_UP;
  608. zlog_warn("%s state -> up : echo response received after %ld.%06ld "
  609. "seconds", dmn->name,
  610. (long)delay.tv_sec, (long)delay.tv_usec);
  611. }
  612. else
  613. zlog_warn("%s: slow echo response finally received after %ld.%06ld "
  614. "seconds", dmn->name,
  615. (long)delay.tv_sec, (long)delay.tv_usec);
  616. }
  617. else if (gs.loglevel > LOG_DEBUG+1)
  618. zlog_debug("%s: echo response received after %ld.%06ld seconds",
  619. dmn->name, (long)delay.tv_sec, (long)delay.tv_usec);
  620. SET_READ_HANDLER(dmn);
  621. if (dmn->t_wakeup)
  622. thread_cancel(dmn->t_wakeup);
  623. SET_WAKEUP_ECHO(dmn);
  624. return 0;
  625. }
  626. static void
  627. daemon_up(struct daemon *dmn, const char *why)
  628. {
  629. dmn->state = DAEMON_UP;
  630. gs.numdown--;
  631. dmn->connect_tries = 0;
  632. zlog_notice("%s state -> up : %s",dmn->name,why);
  633. if (gs.do_ping)
  634. SET_WAKEUP_ECHO(dmn);
  635. phase_check();
  636. }
  637. static int
  638. check_connect(struct thread *t_write)
  639. {
  640. struct daemon *dmn = THREAD_ARG(t_write);
  641. int sockerr;
  642. socklen_t reslen = sizeof(sockerr);
  643. dmn->t_write = NULL;
  644. if (getsockopt(dmn->fd,SOL_SOCKET,SO_ERROR,(char *)&sockerr,&reslen) < 0)
  645. {
  646. zlog_warn("%s: check_connect: getsockopt failed: %s",
  647. dmn->name,safe_strerror(errno));
  648. daemon_down(dmn,"getsockopt failed checking connection success");
  649. return 0;
  650. }
  651. if ((reslen == sizeof(sockerr)) && sockerr)
  652. {
  653. char why[100];
  654. snprintf(why,sizeof(why),
  655. "getsockopt reports that connection attempt failed: %s",
  656. safe_strerror(sockerr));
  657. daemon_down(dmn,why);
  658. return 0;
  659. }
  660. daemon_up(dmn,"delayed connect succeeded");
  661. return 0;
  662. }
  663. static int
  664. wakeup_connect_hanging(struct thread *t_wakeup)
  665. {
  666. struct daemon *dmn = THREAD_ARG(t_wakeup);
  667. char why[100];
  668. dmn->t_wakeup = NULL;
  669. snprintf(why,sizeof(why),"connection attempt timed out after %ld seconds",
  670. gs.timeout);
  671. daemon_down(dmn,why);
  672. return 0;
  673. }
  674. /* Making connection to protocol daemon. */
  675. static int
  676. try_connect(struct daemon *dmn)
  677. {
  678. int sock;
  679. struct sockaddr_un addr;
  680. socklen_t len;
  681. if (gs.loglevel > LOG_DEBUG+1)
  682. zlog_debug("%s: attempting to connect",dmn->name);
  683. dmn->connect_tries++;
  684. memset (&addr, 0, sizeof (struct sockaddr_un));
  685. addr.sun_family = AF_UNIX;
  686. snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s.vty",
  687. gs.vtydir,dmn->name);
  688. #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
  689. len = addr.sun_len = SUN_LEN(&addr);
  690. #else
  691. len = sizeof (addr.sun_family) + strlen (addr.sun_path);
  692. #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
  693. /* Quick check to see if we might succeed before we go to the trouble
  694. of creating a socket. */
  695. if (access(addr.sun_path, W_OK) < 0)
  696. {
  697. if (errno != ENOENT)
  698. zlog_err("%s: access to socket %s denied: %s",
  699. dmn->name,addr.sun_path,safe_strerror(errno));
  700. return -1;
  701. }
  702. if ((sock = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
  703. {
  704. zlog_err("%s(%s): cannot make socket: %s",
  705. __func__,addr.sun_path, safe_strerror(errno));
  706. return -1;
  707. }
  708. if (set_nonblocking(sock) < 0)
  709. {
  710. zlog_err("%s(%s): set_nonblocking(%d) failed",
  711. __func__, addr.sun_path, sock);
  712. close(sock);
  713. return -1;
  714. }
  715. if (connect (sock, (struct sockaddr *) &addr, len) < 0)
  716. {
  717. if ((errno != EINPROGRESS) && (errno != EWOULDBLOCK))
  718. {
  719. if (gs.loglevel > LOG_DEBUG)
  720. zlog_debug("%s(%s): connect failed: %s",
  721. __func__,addr.sun_path, safe_strerror(errno));
  722. close (sock);
  723. return -1;
  724. }
  725. if (gs.loglevel > LOG_DEBUG)
  726. zlog_debug("%s: connection in progress",dmn->name);
  727. dmn->state = DAEMON_CONNECTING;
  728. dmn->fd = sock;
  729. dmn->t_write = thread_add_write(master,check_connect,dmn,dmn->fd);
  730. dmn->t_wakeup = thread_add_timer(master,wakeup_connect_hanging,dmn,
  731. gs.timeout);
  732. SET_READ_HANDLER(dmn);
  733. return 0;
  734. }
  735. dmn->fd = sock;
  736. SET_READ_HANDLER(dmn);
  737. daemon_up(dmn,"connect succeeded");
  738. return 1;
  739. }
  740. static int
  741. phase_hanging(struct thread *t_hanging)
  742. {
  743. gs.t_phase_hanging = NULL;
  744. zlog_err("Phase [%s] hanging for %ld seconds, aborting phased restart",
  745. phase_str[gs.phase],PHASE_TIMEOUT);
  746. gs.phase = PHASE_NONE;
  747. return 0;
  748. }
  749. static void
  750. set_phase(restart_phase_t new_phase)
  751. {
  752. gs.phase = new_phase;
  753. if (gs.t_phase_hanging)
  754. thread_cancel(gs.t_phase_hanging);
  755. gs.t_phase_hanging = thread_add_timer(master,phase_hanging,NULL,
  756. PHASE_TIMEOUT);
  757. }
  758. static void
  759. phase_check(void)
  760. {
  761. switch (gs.phase)
  762. {
  763. case PHASE_NONE:
  764. break;
  765. case PHASE_STOPS_PENDING:
  766. if (gs.numpids)
  767. break;
  768. zlog_info("Phased restart: all routing daemon stop jobs have completed.");
  769. set_phase(PHASE_WAITING_DOWN);
  770. /*FALLTHRU*/
  771. case PHASE_WAITING_DOWN:
  772. if (gs.numdown+IS_UP(gs.special) < gs.numdaemons)
  773. break;
  774. zlog_info("Phased restart: all routing daemons now down.");
  775. run_job(&gs.special->restart,"restart",gs.restart_command,1,1);
  776. set_phase(PHASE_ZEBRA_RESTART_PENDING);
  777. /*FALLTHRU*/
  778. case PHASE_ZEBRA_RESTART_PENDING:
  779. if (gs.special->restart.pid)
  780. break;
  781. zlog_info("Phased restart: %s restart job completed.",gs.special->name);
  782. set_phase(PHASE_WAITING_ZEBRA_UP);
  783. /*FALLTHRU*/
  784. case PHASE_WAITING_ZEBRA_UP:
  785. if (!IS_UP(gs.special))
  786. break;
  787. zlog_info("Phased restart: %s is now up.",gs.special->name);
  788. {
  789. struct daemon *dmn;
  790. for (dmn = gs.daemons; dmn; dmn = dmn->next)
  791. {
  792. if (dmn != gs.special)
  793. run_job(&dmn->restart,"start",gs.start_command,1,0);
  794. }
  795. }
  796. gs.phase = PHASE_NONE;
  797. THREAD_OFF(gs.t_phase_hanging);
  798. zlog_notice("Phased global restart has completed.");
  799. break;
  800. }
  801. }
  802. static void
  803. try_restart(struct daemon *dmn)
  804. {
  805. switch (gs.mode)
  806. {
  807. case MODE_MONITOR:
  808. return;
  809. case MODE_GLOBAL_RESTART:
  810. run_job(&gs.restart,"restart",gs.restart_command,0,1);
  811. break;
  812. case MODE_SEPARATE_RESTART:
  813. run_job(&dmn->restart,"restart",gs.restart_command,0,1);
  814. break;
  815. case MODE_PHASED_ZEBRA_RESTART:
  816. if (dmn != gs.special)
  817. {
  818. if ((gs.special->state == DAEMON_UP) && (gs.phase == PHASE_NONE))
  819. run_job(&dmn->restart,"restart",gs.restart_command,0,1);
  820. else
  821. zlog_debug("%s: postponing restart attempt because master %s daemon "
  822. "not up [%s], or phased restart in progress",
  823. dmn->name,gs.special->name,state_str[gs.special->state]);
  824. break;
  825. }
  826. /*FALLTHRU*/
  827. case MODE_PHASED_ALL_RESTART:
  828. if ((gs.phase != PHASE_NONE) || gs.numpids)
  829. {
  830. if (gs.loglevel > LOG_DEBUG+1)
  831. zlog_debug("postponing phased global restart: restart already in "
  832. "progress [%s], or outstanding child processes [%d]",
  833. phase_str[gs.phase],gs.numpids);
  834. break;
  835. }
  836. /* Is it too soon for a restart? */
  837. {
  838. struct timeval delay;
  839. if (time_elapsed(&delay,&gs.special->restart.time)->tv_sec <
  840. gs.special->restart.interval)
  841. {
  842. if (gs.loglevel > LOG_DEBUG+1)
  843. zlog_debug("postponing phased global restart: "
  844. "elapsed time %ld < retry interval %ld",
  845. (long)delay.tv_sec,gs.special->restart.interval);
  846. break;
  847. }
  848. }
  849. zlog_info("Phased restart: stopping all routing daemons.");
  850. /* First step: stop all other daemons. */
  851. for (dmn = gs.daemons; dmn; dmn = dmn->next)
  852. {
  853. if (dmn != gs.special)
  854. run_job(&dmn->restart,"stop",gs.stop_command,1,1);
  855. }
  856. set_phase(PHASE_STOPS_PENDING);
  857. break;
  858. default:
  859. zlog_err("error: unknown restart mode %d",gs.mode);
  860. break;
  861. }
  862. }
  863. static int
  864. wakeup_unresponsive(struct thread *t_wakeup)
  865. {
  866. struct daemon *dmn = THREAD_ARG(t_wakeup);
  867. dmn->t_wakeup = NULL;
  868. if (dmn->state != DAEMON_UNRESPONSIVE)
  869. zlog_err("%s: no longer unresponsive (now %s), "
  870. "wakeup should have been cancelled!",
  871. dmn->name,state_str[dmn->state]);
  872. else
  873. {
  874. SET_WAKEUP_UNRESPONSIVE(dmn);
  875. try_restart(dmn);
  876. }
  877. return 0;
  878. }
  879. static int
  880. wakeup_no_answer(struct thread *t_wakeup)
  881. {
  882. struct daemon *dmn = THREAD_ARG(t_wakeup);
  883. dmn->t_wakeup = NULL;
  884. dmn->state = DAEMON_UNRESPONSIVE;
  885. zlog_err("%s state -> unresponsive : no response yet to ping "
  886. "sent %ld seconds ago",dmn->name,gs.timeout);
  887. if (gs.unresponsive_restart)
  888. {
  889. SET_WAKEUP_UNRESPONSIVE(dmn);
  890. try_restart(dmn);
  891. }
  892. return 0;
  893. }
  894. static int
  895. wakeup_send_echo(struct thread *t_wakeup)
  896. {
  897. static const char echocmd[] = "echo " PING_TOKEN;
  898. ssize_t rc;
  899. struct daemon *dmn = THREAD_ARG(t_wakeup);
  900. dmn->t_wakeup = NULL;
  901. if (((rc = write(dmn->fd,echocmd,sizeof(echocmd))) < 0) ||
  902. ((size_t)rc != sizeof(echocmd)))
  903. {
  904. char why[100+sizeof(echocmd)];
  905. snprintf(why,sizeof(why),"write '%s' returned %d instead of %u",
  906. echocmd,(int)rc,(u_int)sizeof(echocmd));
  907. daemon_down(dmn,why);
  908. }
  909. else
  910. {
  911. gettimeofday(&dmn->echo_sent,NULL);
  912. dmn->t_wakeup = thread_add_timer(master,wakeup_no_answer,dmn,gs.timeout);
  913. }
  914. return 0;
  915. }
  916. static void
  917. sigint(void)
  918. {
  919. zlog_notice("Terminating on signal");
  920. exit(0);
  921. }
  922. static int
  923. valid_command(const char *cmd)
  924. {
  925. char *p;
  926. return ((p = strchr(cmd,'%')) != NULL) && (*(p+1) == 's') && !strchr(p+1,'%');
  927. }
  928. /* This is an ugly hack to circumvent problems with passing command-line
  929. arguments that contain spaces. The fix is to use a configuration file. */
  930. static char *
  931. translate_blanks(const char *cmd, const char *blankstr)
  932. {
  933. char *res;
  934. char *p;
  935. size_t bslen = strlen(blankstr);
  936. if (!(res = strdup(cmd)))
  937. {
  938. perror("strdup");
  939. exit(1);
  940. }
  941. while ((p = strstr(res,blankstr)) != NULL)
  942. {
  943. *p = ' ';
  944. if (bslen != 1)
  945. memmove(p+1,p+bslen,strlen(p+bslen)+1);
  946. }
  947. return res;
  948. }
  949. int
  950. main(int argc, char **argv)
  951. {
  952. const char *progname;
  953. int opt;
  954. int daemon_mode = 0;
  955. const char *pidfile = DEFAULT_PIDFILE;
  956. const char *special = "zebra";
  957. const char *blankstr = NULL;
  958. static struct quagga_signal_t my_signals[] =
  959. {
  960. {
  961. .signal = SIGINT,
  962. .handler = sigint,
  963. },
  964. {
  965. .signal = SIGTERM,
  966. .handler = sigint,
  967. },
  968. {
  969. .signal = SIGCHLD,
  970. .handler = sigchild,
  971. },
  972. };
  973. if ((progname = strrchr (argv[0], '/')) != NULL)
  974. progname++;
  975. else
  976. progname = argv[0];
  977. gs.restart.name = "all";
  978. while ((opt = getopt_long(argc, argv, "aAb:dek:l:m:M:i:p:r:R:S:s:t:T:zvh",
  979. longopts, 0)) != EOF)
  980. {
  981. switch (opt)
  982. {
  983. case 0:
  984. break;
  985. case 'a':
  986. if ((gs.mode != MODE_MONITOR) && (gs.mode != MODE_SEPARATE_RESTART))
  987. {
  988. fputs("Ambiguous operating mode selected.\n",stderr);
  989. return usage(progname,1);
  990. }
  991. gs.mode = MODE_PHASED_ZEBRA_RESTART;
  992. break;
  993. case 'A':
  994. if ((gs.mode != MODE_MONITOR) && (gs.mode != MODE_SEPARATE_RESTART))
  995. {
  996. fputs("Ambiguous operating mode selected.\n",stderr);
  997. return usage(progname,1);
  998. }
  999. gs.mode = MODE_PHASED_ALL_RESTART;
  1000. break;
  1001. case 'b':
  1002. blankstr = optarg;
  1003. break;
  1004. case 'd':
  1005. daemon_mode = 1;
  1006. break;
  1007. case 'e':
  1008. gs.do_ping = 0;
  1009. break;
  1010. case 'k':
  1011. if (!valid_command(optarg))
  1012. {
  1013. fprintf(stderr,"Invalid kill command, must contain '%%s': %s\n",
  1014. optarg);
  1015. return usage(progname,1);
  1016. }
  1017. gs.stop_command = optarg;
  1018. break;
  1019. case 'l':
  1020. {
  1021. char garbage[3];
  1022. if ((sscanf(optarg,"%d%1s",&gs.loglevel,garbage) != 1) ||
  1023. (gs.loglevel < LOG_EMERG))
  1024. {
  1025. fprintf(stderr,"Invalid loglevel argument: %s\n",optarg);
  1026. return usage(progname,1);
  1027. }
  1028. }
  1029. break;
  1030. case 'm':
  1031. {
  1032. char garbage[3];
  1033. if ((sscanf(optarg,"%ld%1s",
  1034. &gs.min_restart_interval,garbage) != 1) ||
  1035. (gs.min_restart_interval < 0))
  1036. {
  1037. fprintf(stderr,"Invalid min_restart_interval argument: %s\n",
  1038. optarg);
  1039. return usage(progname,1);
  1040. }
  1041. }
  1042. break;
  1043. case 'M':
  1044. {
  1045. char garbage[3];
  1046. if ((sscanf(optarg,"%ld%1s",
  1047. &gs.max_restart_interval,garbage) != 1) ||
  1048. (gs.max_restart_interval < 0))
  1049. {
  1050. fprintf(stderr,"Invalid max_restart_interval argument: %s\n",
  1051. optarg);
  1052. return usage(progname,1);
  1053. }
  1054. }
  1055. break;
  1056. case 'i':
  1057. {
  1058. char garbage[3];
  1059. int period;
  1060. if ((sscanf(optarg,"%d%1s",&period,garbage) != 1) ||
  1061. (gs.period < 1))
  1062. {
  1063. fprintf(stderr,"Invalid interval argument: %s\n",optarg);
  1064. return usage(progname,1);
  1065. }
  1066. gs.period = 1000*period;
  1067. }
  1068. break;
  1069. case 'p':
  1070. pidfile = optarg;
  1071. break;
  1072. case 'r':
  1073. if ((gs.mode == MODE_GLOBAL_RESTART) ||
  1074. (gs.mode == MODE_SEPARATE_RESTART))
  1075. {
  1076. fputs("Ambiguous operating mode selected.\n",stderr);
  1077. return usage(progname,1);
  1078. }
  1079. if (!valid_command(optarg))
  1080. {
  1081. fprintf(stderr,
  1082. "Invalid restart command, must contain '%%s': %s\n",
  1083. optarg);
  1084. return usage(progname,1);
  1085. }
  1086. gs.restart_command = optarg;
  1087. if (gs.mode == MODE_MONITOR)
  1088. gs.mode = MODE_SEPARATE_RESTART;
  1089. break;
  1090. case 'R':
  1091. if (gs.mode != MODE_MONITOR)
  1092. {
  1093. fputs("Ambiguous operating mode selected.\n",stderr);
  1094. return usage(progname,1);
  1095. }
  1096. if (strchr(optarg,'%'))
  1097. {
  1098. fprintf(stderr,
  1099. "Invalid restart-all arg, must not contain '%%s': %s\n",
  1100. optarg);
  1101. return usage(progname,1);
  1102. }
  1103. gs.restart_command = optarg;
  1104. gs.mode = MODE_GLOBAL_RESTART;
  1105. break;
  1106. case 's':
  1107. if (!valid_command(optarg))
  1108. {
  1109. fprintf(stderr,"Invalid start command, must contain '%%s': %s\n",
  1110. optarg);
  1111. return usage(progname,1);
  1112. }
  1113. gs.start_command = optarg;
  1114. break;
  1115. case 'S':
  1116. gs.vtydir = optarg;
  1117. break;
  1118. case 't':
  1119. {
  1120. char garbage[3];
  1121. if ((sscanf(optarg,"%ld%1s",&gs.timeout,garbage) != 1) ||
  1122. (gs.timeout < 1))
  1123. {
  1124. fprintf(stderr,"Invalid timeout argument: %s\n",optarg);
  1125. return usage(progname,1);
  1126. }
  1127. }
  1128. break;
  1129. case 'T':
  1130. {
  1131. char garbage[3];
  1132. if ((sscanf(optarg,"%ld%1s",&gs.restart_timeout,garbage) != 1) ||
  1133. (gs.restart_timeout < 1))
  1134. {
  1135. fprintf(stderr,"Invalid restart timeout argument: %s\n",optarg);
  1136. return usage(progname,1);
  1137. }
  1138. }
  1139. break;
  1140. case 'z':
  1141. gs.unresponsive_restart = 1;
  1142. break;
  1143. case 'v':
  1144. printf ("%s version %s\n", progname, QUAGGA_VERSION);
  1145. puts("Copyright 2004 Andrew J. Schorr");
  1146. return 0;
  1147. case 'h':
  1148. return usage(progname,0);
  1149. default:
  1150. fputs("Invalid option.\n",stderr);
  1151. return usage(progname,1);
  1152. }
  1153. }
  1154. if (gs.unresponsive_restart && (gs.mode == MODE_MONITOR))
  1155. {
  1156. fputs("Option -z requires a -r or -R restart option.\n",stderr);
  1157. return usage(progname,1);
  1158. }
  1159. switch (gs.mode)
  1160. {
  1161. case MODE_MONITOR:
  1162. if (gs.restart_command || gs.start_command || gs.stop_command)
  1163. {
  1164. fprintf(stderr,"No kill/(re)start commands needed for %s mode.\n",
  1165. mode_str[gs.mode]);
  1166. return usage(progname,1);
  1167. }
  1168. break;
  1169. case MODE_GLOBAL_RESTART:
  1170. case MODE_SEPARATE_RESTART:
  1171. if (!gs.restart_command || gs.start_command || gs.stop_command)
  1172. {
  1173. fprintf(stderr,"No start/kill commands needed in [%s] mode.\n",
  1174. mode_str[gs.mode]);
  1175. return usage(progname,1);
  1176. }
  1177. break;
  1178. case MODE_PHASED_ZEBRA_RESTART:
  1179. case MODE_PHASED_ALL_RESTART:
  1180. if (!gs.restart_command || !gs.start_command || !gs.stop_command)
  1181. {
  1182. fprintf(stderr,
  1183. "Need start, kill, and restart commands in [%s] mode.\n",
  1184. mode_str[gs.mode]);
  1185. return usage(progname,1);
  1186. }
  1187. break;
  1188. }
  1189. if (blankstr)
  1190. {
  1191. if (gs.restart_command)
  1192. gs.restart_command = translate_blanks(gs.restart_command,blankstr);
  1193. if (gs.start_command)
  1194. gs.start_command = translate_blanks(gs.start_command,blankstr);
  1195. if (gs.stop_command)
  1196. gs.stop_command = translate_blanks(gs.stop_command,blankstr);
  1197. }
  1198. gs.restart.interval = gs.min_restart_interval;
  1199. master = thread_master_create();
  1200. signal_init (master, array_size(my_signals), my_signals);
  1201. srandom(time(NULL));
  1202. {
  1203. int i;
  1204. struct daemon *tail = NULL;
  1205. for (i = optind; i < argc; i++)
  1206. {
  1207. struct daemon *dmn;
  1208. if (!(dmn = (struct daemon *)calloc(1,sizeof(*dmn))))
  1209. {
  1210. fprintf(stderr,"calloc(1,%u) failed: %s\n",
  1211. (u_int)sizeof(*dmn), safe_strerror(errno));
  1212. return 1;
  1213. }
  1214. dmn->name = dmn->restart.name = argv[i];
  1215. dmn->state = DAEMON_INIT;
  1216. gs.numdaemons++;
  1217. gs.numdown++;
  1218. dmn->fd = -1;
  1219. dmn->t_wakeup = thread_add_timer_msec(master,wakeup_init,dmn,
  1220. 100+(random() % 900));
  1221. dmn->restart.interval = gs.min_restart_interval;
  1222. if (tail)
  1223. tail->next = dmn;
  1224. else
  1225. gs.daemons = dmn;
  1226. tail = dmn;
  1227. if (((gs.mode == MODE_PHASED_ZEBRA_RESTART) ||
  1228. (gs.mode == MODE_PHASED_ALL_RESTART)) &&
  1229. !strcmp(dmn->name,special))
  1230. gs.special = dmn;
  1231. }
  1232. }
  1233. if (!gs.daemons)
  1234. {
  1235. fputs("Must specify one or more daemons to monitor.\n",stderr);
  1236. return usage(progname,1);
  1237. }
  1238. if (((gs.mode == MODE_PHASED_ZEBRA_RESTART) ||
  1239. (gs.mode == MODE_PHASED_ALL_RESTART)) && !gs.special)
  1240. {
  1241. fprintf(stderr,"In mode [%s], but cannot find master daemon %s\n",
  1242. mode_str[gs.mode],special);
  1243. return usage(progname,1);
  1244. }
  1245. if (gs.special && (gs.numdaemons < 2))
  1246. {
  1247. fprintf(stderr,"Mode [%s] does not make sense with only 1 daemon "
  1248. "to watch.\n",mode_str[gs.mode]);
  1249. return usage(progname,1);
  1250. }
  1251. zlog_default = openzlog(progname, ZLOG_NONE,
  1252. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  1253. zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
  1254. if (daemon_mode)
  1255. {
  1256. zlog_set_level(NULL, ZLOG_DEST_SYSLOG, MIN(gs.loglevel,LOG_DEBUG));
  1257. if (daemon (0, 0) < 0)
  1258. {
  1259. fprintf(stderr, "Watchquagga daemon failed: %s", strerror(errno));
  1260. exit (1);
  1261. }
  1262. }
  1263. else
  1264. zlog_set_level(NULL, ZLOG_DEST_STDOUT, MIN(gs.loglevel,LOG_DEBUG));
  1265. /* Make sure we're not already running. */
  1266. pid_output (pidfile);
  1267. /* Announce which daemons are being monitored. */
  1268. {
  1269. struct daemon *dmn;
  1270. size_t len = 0;
  1271. for (dmn = gs.daemons; dmn; dmn = dmn->next)
  1272. len += strlen(dmn->name)+1;
  1273. {
  1274. char buf[len+1];
  1275. char *p = buf;
  1276. for (dmn = gs.daemons; dmn; dmn = dmn->next)
  1277. {
  1278. if (p != buf)
  1279. *p++ = ' ';
  1280. strcpy(p,dmn->name);
  1281. p += strlen(p);
  1282. }
  1283. zlog_notice("%s %s watching [%s], mode [%s]",
  1284. progname, QUAGGA_VERSION, buf, mode_str[gs.mode]);
  1285. }
  1286. }
  1287. thread_main (master);
  1288. /* Not reached. */
  1289. return 0;
  1290. }