defs.h (3183B)
1 #pragma once 2 3 #include "types.h" 4 5 #include <stdbool.h> 6 #include <time.h> 7 8 #define PID_BUFFER_MAX 16 9 #define STAT_BUFFER_MAX 128 10 11 #define WANT_ROTATE(current, next) \ 12 (current) != (next) && IS_ACTIVE(current) != IS_ACTIVE(next) 13 14 15 typedef enum command { 16 X_UP = 'u', // starts the services, pin as started 17 X_DOWN = 'd', // stops the service, pin as stopped 18 X_ONCE = 'o', // starts the service, pin as once 19 X_TERM = 't', // same as down 20 X_KILL = 'k', // sends kill, pin as stopped 21 X_PAUSE = 'p', // pauses the service 22 X_CONT = 'c', // resumes the service 23 X_RESET = 'r', // resets the service 24 X_ALARM = 'a', // sends alarm 25 X_HUP = 'h', // sends hup 26 X_INT = 'i', // sends interrupt 27 X_QUIT = 'q', // sends quit 28 X_USR1 = '1', // sends usr1 29 X_USR2 = '2', // sends usr2 30 X_EXIT = 'x', // does nothing 31 } command_t; 32 33 34 typedef enum state { 35 STATE_INACTIVE, // not started 36 STATE_DEPENDENCY, // waiting for dependencies 37 STATE_SETUP, // ./setup running 38 STATE_STARTING, // ./start running 39 STATE_READY, // ./ready running, waiting for finish 40 STATE_ACTIVE_FOREGROUND, // ./run running 41 STATE_ACTIVE_BACKGROUND, // ./start finished, ./stop not called yet 42 STATE_ACTIVE_PID, // ./start finished, waiting for ./pid 43 STATE_ACTIVE_DUMMY, // dependencies started 44 STATE_STOPPING, // ./stop running 45 STATE_FINISHING, // ./finish running 46 } state_t; 47 48 #define STATE_MAX STATE_FINISHING 49 50 typedef struct serial { 51 u8 status_change[8]; 52 u8 state; 53 u8 return_code; 54 u8 fail_count; 55 u8 flags; 56 u8 pid[4]; 57 u8 paused; 58 u8 restart; 59 u8 force_down; 60 u8 state_runit; 61 } serial_t; 62 63 typedef struct service { 64 char name[NAME_MAX]; // name of service 65 state_t state; // current state 66 state_t desired_state; // should update state (inactive -> setup -> ...) 67 time_t state_change; // last status change 68 time_t desired_state_change; // last desired status change 69 bool should_restart; // if inactive, push active 70 pid_t pid; // pid of run 71 u8 dependent_count; // count of services depends on me 72 u32 death_count; // current fail cound 73 i32 death_last; // last death (waitstatus) 74 bool is_log_service; // is a log service 75 bool paused; // is paused 76 time_t stop_timeout; // stop start-time 77 pipe_t log_pipe; // pipe for logging 78 bool has_log_service; // has a log_server otherwise NULL 79 struct { // fd's of supervise/* 80 i32 control; // fd of control 81 i32 ok; // fd of ok 82 i32 lock; // fd of lock 83 i32 dependent; // fd of depends 84 } supervise; 85 } service_t; 86 87 extern service_t service; 88 89 const char* state_name(state_t state); 90 void encode(serial_t* buffer); 91 int write_status(void); 92 void control_loop(void); 93 state_t get_rotated_state(void); 94 int rotate_state(bool now); 95 bool need_restart(void);