hmain.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #ifndef HV_MAIN_H_
  2. #define HV_MAIN_H_
  3. #include "hexport.h"
  4. #include "hplatform.h"
  5. #include "hdef.h"
  6. #include "hproc.h"
  7. #ifdef _MSC_VER
  8. #pragma comment(lib, "winmm.lib") // for timeSetEvent
  9. #endif
  10. BEGIN_EXTERN_C
  11. typedef int (*printf_t)(const char *const fmt, ...);
  12. typedef struct main_ctx_s {
  13. char run_dir[MAX_PATH];
  14. char program_name[MAX_PATH];
  15. char confile[MAX_PATH]; // default etc/${program}.conf
  16. char pidfile[MAX_PATH]; // default logs/${program}.pid
  17. char logfile[MAX_PATH]; // default logs/${program}.log
  18. pid_t pid; // getpid
  19. pid_t oldpid; // getpid_from_pidfile
  20. // arg
  21. int argc;
  22. int arg_len;
  23. char** os_argv;
  24. char** save_argv;
  25. char* cmdline;
  26. // parsed arg
  27. int arg_kv_size;
  28. char** arg_kv;
  29. int arg_list_size;
  30. char** arg_list;
  31. // env
  32. int envc;
  33. int env_len;
  34. char** os_envp;
  35. char** save_envp;
  36. // signals
  37. procedure_t reload_fn;
  38. void* reload_userdata;
  39. // master workers model
  40. int worker_processes;
  41. int worker_threads;
  42. procedure_t worker_fn;
  43. void* worker_userdata;
  44. proc_ctx_t* proc_ctxs;
  45. } main_ctx_t;
  46. // arg_type
  47. #define NO_ARGUMENT 0
  48. #define REQUIRED_ARGUMENT 1
  49. #define OPTIONAL_ARGUMENT 2
  50. // option define
  51. #define OPTION_PREFIX '-'
  52. #define OPTION_DELIM '='
  53. #define OPTION_ENABLE "1"
  54. #define OPTION_DISABLE "0"
  55. typedef struct option_s {
  56. char short_opt;
  57. const char* long_opt;
  58. int arg_type;
  59. const char* description;
  60. } option_t;
  61. HV_EXPORT int main_ctx_init(int argc, char** argv);
  62. HV_EXPORT void main_ctx_free(void);
  63. // ls -a -l
  64. // ls -al
  65. // watch -n 10 ls
  66. // watch -n10 ls
  67. HV_EXPORT int parse_opt(int argc, char** argv, const char* opt);
  68. // gcc -g -Wall -O3 -std=cpp main.c
  69. HV_EXPORT int parse_opt_long(int argc, char** argv, const option_t* long_options, int opt_size);
  70. HV_EXPORT int dump_opt_long(const option_t* long_options, int opt_size, char* out_str, int out_size);
  71. HV_EXPORT const char* get_arg(const char* key);
  72. HV_EXPORT const char* get_env(const char* key);
  73. #if defined(OS_UNIX) && !HAVE_SETPROCTITLE
  74. HV_EXPORT void setproctitle(const char* fmt, ...);
  75. #endif
  76. // pidfile
  77. HV_EXPORT int create_pidfile();
  78. HV_EXPORT void delete_pidfile(void);
  79. HV_EXPORT pid_t getpid_from_pidfile();
  80. // signal=[start,stop,restart,status,reload]
  81. HV_EXPORT int signal_init(procedure_t reload_fn DEFAULT(NULL), void* reload_userdata DEFAULT(NULL));
  82. HV_EXPORT void signal_handle(const char* signal);
  83. HV_EXPORT bool signal_handle_noexit(const char* signal);
  84. #ifdef OS_UNIX
  85. // we use SIGTERM to quit process, SIGUSR1 to reload confile
  86. #define SIGNAL_TERMINATE SIGTERM
  87. #define SIGNAL_RELOAD SIGUSR1
  88. void signal_handler(int signo);
  89. #endif
  90. // global var
  91. #define DEFAULT_WORKER_PROCESSES 4
  92. #define MAXNUM_WORKER_PROCESSES 256
  93. HV_EXPORT extern main_ctx_t g_main_ctx;
  94. HV_EXPORT extern printf_t printf_fn;
  95. // master-workers processes
  96. HV_EXPORT int master_workers_run(
  97. procedure_t worker_fn,
  98. void* worker_userdata DEFAULT(NULL),
  99. int worker_processes DEFAULT(DEFAULT_WORKER_PROCESSES),
  100. int worker_threads DEFAULT(0),
  101. bool wait DEFAULT(true));
  102. END_EXTERN_C
  103. #endif // HV_MAIN_H_