fiss

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

service.h (4443B)


      1 #pragma once
      2 
      3 #include "../../config.h"
      4 #include "types.h"
      5 #include "util.h"
      6 
      7 #include <stdbool.h>
      8 #include <stdint.h>
      9 #include <time.h>
     10 
     11 
     12 enum service_command {
     13 	X_UP    = 'u',    // starts the services, pin as started
     14 	X_DOWN  = 'd',    // stops the service, pin as stopped
     15 	X_ONCE  = 'o',    // starts the service, pin as once
     16 	X_TERM  = 't',    // same as down
     17 	X_KILL  = 'k',    // sends kill, pin as stopped
     18 	X_PAUSE = 'p',    // pauses the service
     19 	X_CONT  = 'c',    // resumes the service
     20 	X_RESET = 'r',    // resets the service
     21 	X_ALARM = 'a',    // sends alarm
     22 	X_HUP   = 'h',    // sends hup
     23 	X_INT   = 'i',    // sends interrupt
     24 	X_QUIT  = 'q',    // sends quit
     25 	X_USR1  = '1',    // sends usr1
     26 	X_USR2  = '2',    // sends usr2
     27 	X_EXIT  = 'x',    // does nothing
     28 };
     29 
     30 enum service_state {
     31 	STATE_INACTIVE,             // not started
     32 	STATE_SETUP,                // ./setup running
     33 	STATE_STARTING,             // ./start running
     34 	STATE_ACTIVE_FOREGROUND,    // ./run running
     35 	STATE_ACTIVE_BACKGROUND,    // ./start finished, ./stop not called yet
     36 	STATE_ACTIVE_DUMMY,         // dependencies started
     37 	STATE_STOPPING,             // ./stop running
     38 	STATE_FINISHING,            // ./finish running
     39 	STATE_DONE,                 // ./stop finished
     40 	STATE_ERROR,                // something went wrong
     41 };
     42 
     43 enum service_exit {
     44 	EXIT_NONE,        // never exited
     45 	EXIT_NORMAL,      // exited
     46 	EXIT_SIGNALED,    // crashed
     47 };
     48 
     49 enum service_restart {
     50 	S_DOWN,       // service should not be started
     51 	S_ONCE,       // service should
     52 	S_RESTART,    // service should be started
     53 };
     54 
     55 struct service_serial {
     56 	uint8_t status_change[8];
     57 	uint8_t state;
     58 	uint8_t return_code;
     59 	uint8_t fail_count;
     60 	uint8_t flags;
     61 	uint8_t pid[4];
     62 	uint8_t paused;
     63 	uint8_t restart;
     64 	uint8_t force_down;
     65 	uint8_t state_runit;
     66 };
     67 
     68 struct service {
     69 	char                 name[NAME_MAX];    // name of service
     70 	enum service_state   state;             // current state
     71 	pid_t                pid;               // pid of run
     72 	int                  dir;               // dirfd
     73 	int                  control;           // fd to supervise/control
     74 	time_t               state_change;      // last status change
     75 	enum service_restart restart;           // should restart on exit
     76 	enum service_exit    last_exit;         // stopped signaled or exited
     77 	int                  return_code;       // return code or signal
     78 	uint8_t              fail_count;        // current fail cound
     79 	bool                 is_log_service;    // is a log service
     80 	bool                 paused;            // is paused
     81 	time_t               stop_timeout;      // stop start-time
     82 	pipe_t               log_pipe;          // pipe for logging
     83 	struct service*      log_service;       // has a log_server otherwise NULL
     84 	int                  parents_size;      // count of service depending on
     85 	struct service*      parents[10];       // service depending on
     86 	int                  children_size;     // count of dependencies
     87 	struct service*      children[10];      // dependencies
     88 };
     89 
     90 extern struct service services[];
     91 extern int            services_size;
     92 extern int            null_fd;
     93 extern bool           daemon_running;
     94 extern const char*    service_dir_path;
     95 extern int            service_dir;
     96 
     97 
     98 void            service_encode(struct service* s, struct service_serial* buffer);
     99 struct service* service_get(const char* name);
    100 void            service_handle_command(struct service* s, char command);
    101 void            service_handle_exit(struct service* s, bool signaled, int return_code);
    102 void            service_kill(struct service* s, int signal);
    103 bool            service_need_restart(struct service* s);
    104 int             service_refresh_directory(void);
    105 struct service* service_register(int dir, const char* name, bool is_log_service);
    106 void            service_run(struct service* s);
    107 int             service_send_command(char command, char extra, const char* service, struct service* response, int response_max);
    108 void            service_start(struct service* s);
    109 const char*     service_status_name(struct service* s);
    110 void            service_stop(struct service* s);
    111 int             service_supervise(const char* service_dir_, const char* service, bool once);
    112 void            service_update_dependency(struct service* s);
    113 bool            service_is_dependency(struct service* s);
    114 void            service_update_state(struct service* s, int state);
    115 void            service_write(struct service* s);