fiss

Friedel's Initialization and Service Supervision
Log | Files | Refs | LICENSE

handle_command.c (1322B)


      1 #include "service.h"
      2 
      3 #include <fcntl.h>
      4 #include <limits.h>
      5 #include <signal.h>
      6 #include <stdio.h>
      7 #include <string.h>
      8 #include <unistd.h>
      9 
     10 
     11 static int runit_signals[] = {
     12 	[X_ALARM] = SIGALRM,
     13 	[X_HUP]   = SIGHUP,
     14 	[X_INT]   = SIGINT,
     15 	[X_QUIT]  = SIGQUIT,
     16 	[X_USR1]  = SIGUSR1,
     17 	[X_USR2]  = SIGUSR2,
     18 };
     19 
     20 void service_handle_command(struct service* s, char command) {
     21 	switch ((enum service_command) command) {
     22 		case X_UP:
     23 			s->restart = S_RESTART;
     24 			service_start(s);
     25 			break;
     26 		case X_ONCE:
     27 			s->restart = S_ONCE;
     28 			service_start(s);
     29 			break;
     30 		case X_DOWN:
     31 		case X_TERM:
     32 			s->restart = S_DOWN;
     33 			service_stop(s);
     34 			break;
     35 		case X_KILL:
     36 			s->restart = S_DOWN;
     37 			service_kill(s, SIGKILL);
     38 			break;
     39 		case X_PAUSE:
     40 			if (!s->paused) {
     41 				s->paused = true;
     42 				service_kill(s, SIGSTOP);
     43 			}
     44 			break;
     45 		case X_CONT:
     46 			if (s->paused) {
     47 				s->paused = false;
     48 				service_kill(s, SIGCONT);
     49 			}
     50 			break;
     51 		case X_ALARM:
     52 		case X_HUP:
     53 		case X_INT:
     54 		case X_QUIT:
     55 		case X_USR1:
     56 		case X_USR2:
     57 			service_kill(s, runit_signals[(int) command]);
     58 			break;
     59 		case X_RESET:
     60 			if (s->paused) {
     61 				s->paused = false;
     62 				service_kill(s, SIGCONT);
     63 			}
     64 
     65 			s->fail_count = 0;
     66 			if (s->state == STATE_ERROR)
     67 				service_update_state(s, STATE_INACTIVE);
     68 
     69 			break;
     70 		case X_EXIT:
     71 			// ignored
     72 			return;
     73 	}
     74 }