| 3 |
* main |
* main |
| 4 |
* - entry of server program |
* - entry of server program |
| 5 |
* |
* |
| 6 |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
* Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com> |
| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
| 34 |
#include <sys/types.h> |
#include <sys/types.h> |
| 35 |
#include <sys/wait.h> |
#include <sys/wait.h> |
| 36 |
|
|
| 37 |
static inline void app_help(void) |
typedef void (*arg_option_handler_t)(void); |
| 38 |
|
|
| 39 |
|
struct arg_option_t |
| 40 |
|
{ |
| 41 |
|
const char *short_arg; |
| 42 |
|
const char *long_arg; |
| 43 |
|
const char *description; |
| 44 |
|
arg_option_handler_t handler; |
| 45 |
|
}; |
| 46 |
|
typedef struct arg_option_t ARG_OPTION; |
| 47 |
|
|
| 48 |
|
static void arg_foreground(void); |
| 49 |
|
static void arg_help(void); |
| 50 |
|
static void arg_version(void); |
| 51 |
|
static void arg_display_log(void); |
| 52 |
|
static void arg_display_error_log(void); |
| 53 |
|
static void arg_compile_info(void); |
| 54 |
|
static void arg_error(void); |
| 55 |
|
|
| 56 |
|
static const ARG_OPTION arg_options[] = { |
| 57 |
|
{"-f", "--foreground", "Run in foreground", arg_foreground}, |
| 58 |
|
{"-h", "--help", "Display help message", arg_help}, |
| 59 |
|
{"-v", "--version", "Display version information", arg_version}, |
| 60 |
|
{NULL, "--display-log", "Display standard log information", arg_display_log}, |
| 61 |
|
{NULL, "--display-error-log", "Display error log information", arg_display_error_log}, |
| 62 |
|
{"-C", "--compile-config", "Display compile configuration", arg_compile_info}}; |
| 63 |
|
|
| 64 |
|
static const int arg_options_count = sizeof(arg_options) / sizeof(ARG_OPTION); |
| 65 |
|
|
| 66 |
|
static int daemon_service = 1; |
| 67 |
|
static int std_log_redir = 0; |
| 68 |
|
static int error_log_redir = 0; |
| 69 |
|
|
| 70 |
|
static void arg_foreground(void) |
| 71 |
{ |
{ |
| 72 |
fprintf(stderr, "Usage: bbsd [-fhv] [...]\n\n" |
daemon_service = 0; |
|
"-f\t--foreground\t\tForce program run in foreground\n" |
|
|
"-h\t--help\t\t\tDisplay this help message\n" |
|
|
"-v\t--version\t\tDisplay version information\n" |
|
|
"\t--display-log\t\tDisplay standard log information\n" |
|
|
"\t--display-error-log\tDisplay error log information\n" |
|
|
"-C\t--compile-config\tDisplay compile configuration\n" |
|
|
"\n If meet any bug, please report to <leaflet@leafok.com>\n\n"); |
|
| 73 |
} |
} |
| 74 |
|
|
| 75 |
static inline void arg_error(void) |
static void arg_help(void) |
| 76 |
{ |
{ |
| 77 |
fprintf(stderr, "Invalid arguments\n"); |
fprintf(stderr, "Usage: bbsd [-fhv] [...]\n\n"); |
| 78 |
app_help(); |
|
| 79 |
|
for (int i = 0; i < arg_options_count; i++) |
| 80 |
|
{ |
| 81 |
|
fprintf(stderr, "%s%*s%s%*s%s\n", |
| 82 |
|
arg_options[i].short_arg ? arg_options[i].short_arg : "", |
| 83 |
|
8 - (arg_options[i].short_arg ? (int)strlen(arg_options[i].short_arg) : 0), "", |
| 84 |
|
arg_options[i].long_arg ? arg_options[i].long_arg : "", |
| 85 |
|
24 - (arg_options[i].long_arg ? (int)strlen(arg_options[i].long_arg) : 0), "", |
| 86 |
|
arg_options[i].description ? arg_options[i].description : ""); |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
fprintf(stderr, "\n If meet any bug, please report to <leaflet@leafok.com>\n\n"); |
| 90 |
|
} |
| 91 |
|
|
| 92 |
|
static void arg_version(void) |
| 93 |
|
{ |
| 94 |
|
printf("%s\n", APP_INFO); |
| 95 |
|
printf("%s\n", COPYRIGHT_INFO); |
| 96 |
} |
} |
| 97 |
|
|
| 98 |
static inline void app_compile_info(void) |
static void arg_display_log(void) |
| 99 |
|
{ |
| 100 |
|
std_log_redir = 1; |
| 101 |
|
} |
| 102 |
|
|
| 103 |
|
static void arg_display_error_log(void) |
| 104 |
|
{ |
| 105 |
|
error_log_redir = 1; |
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
static void arg_compile_info(void) |
| 109 |
{ |
{ |
| 110 |
printf("%s\n" |
printf("%s\n" |
| 111 |
"--enable-shared\t\t[%s]\n" |
"--enable-shared\t\t[%s]\n" |
| 148 |
); |
); |
| 149 |
} |
} |
| 150 |
|
|
| 151 |
|
static void arg_error(void) |
| 152 |
|
{ |
| 153 |
|
fprintf(stderr, "Invalid arguments\n"); |
| 154 |
|
arg_help(); |
| 155 |
|
} |
| 156 |
|
|
| 157 |
int main(int argc, char *argv[]) |
int main(int argc, char *argv[]) |
| 158 |
{ |
{ |
| 159 |
char file_path_temp[FILE_PATH_LEN]; |
char file_path_temp[FILE_PATH_LEN]; |
|
int daemon = 1; |
|
|
int std_log_redir = 0; |
|
|
int error_log_redir = 0; |
|
| 160 |
FILE *fp; |
FILE *fp; |
| 161 |
int ret; |
int ret; |
| 162 |
int last_aid; |
int last_aid; |
| 168 |
// Parse args |
// Parse args |
| 169 |
for (i = 1; i < argc; i++) |
for (i = 1; i < argc; i++) |
| 170 |
{ |
{ |
| 171 |
switch (argv[i][0]) |
for (j = 0; j < arg_options_count; j++) |
| 172 |
{ |
{ |
| 173 |
case '-': |
if (arg_options[j].short_arg && strcmp(argv[i], arg_options[j].short_arg) == 0) |
|
if (argv[i][1] != '-') |
|
| 174 |
{ |
{ |
| 175 |
for (j = 1; j < strlen(argv[i]); j++) |
arg_options[j].handler(); |
| 176 |
{ |
break; |
|
switch (argv[i][j]) |
|
|
{ |
|
|
case 'f': |
|
|
daemon = 0; |
|
|
break; |
|
|
case 'h': |
|
|
app_help(); |
|
|
return 0; |
|
|
case 'v': |
|
|
printf("%s\n", APP_INFO); |
|
|
printf("%s\n", COPYRIGHT_INFO); |
|
|
return 0; |
|
|
case 'C': |
|
|
app_compile_info(); |
|
|
return 0; |
|
|
default: |
|
|
arg_error(); |
|
|
return 1; |
|
|
} |
|
|
} |
|
| 177 |
} |
} |
| 178 |
else |
else if (arg_options[j].long_arg && strcmp(argv[i], arg_options[j].long_arg) == 0) |
| 179 |
{ |
{ |
| 180 |
if (strcmp(argv[i] + 2, "foreground") == 0) |
arg_options[j].handler(); |
| 181 |
{ |
break; |
|
daemon = 0; |
|
|
break; |
|
|
} |
|
|
if (strcmp(argv[i] + 2, "help") == 0) |
|
|
{ |
|
|
app_help(); |
|
|
return 0; |
|
|
} |
|
|
if (strcmp(argv[i] + 2, "version") == 0) |
|
|
{ |
|
|
printf("%s\n", APP_INFO); |
|
|
printf("%s\n", COPYRIGHT_INFO); |
|
|
return 0; |
|
|
} |
|
|
if (strcmp(argv[i] + 2, "display-log") == 0) |
|
|
{ |
|
|
std_log_redir = 1; |
|
|
} |
|
|
if (strcmp(argv[i] + 2, "display-error-log") == 0) |
|
|
{ |
|
|
error_log_redir = 1; |
|
|
} |
|
|
if (strcmp(argv[i] + 2, "compile-config") == 0) |
|
|
{ |
|
|
app_compile_info(); |
|
|
return 0; |
|
|
} |
|
| 182 |
} |
} |
| 183 |
break; |
} |
| 184 |
|
if (j == arg_options_count) |
| 185 |
|
{ |
| 186 |
|
arg_error(); |
| 187 |
|
return -1; |
| 188 |
} |
} |
| 189 |
} |
} |
| 190 |
|
|
| 191 |
// Initialize daemon |
// Initialize daemon |
| 192 |
if (daemon) |
if (daemon_service) |
| 193 |
{ |
{ |
| 194 |
init_daemon(); |
init_daemon(); |
| 195 |
} |
} |
| 222 |
return -1; |
return -1; |
| 223 |
} |
} |
| 224 |
|
|
| 225 |
if ((!daemon) && std_log_redir) |
if ((!daemon_service) && std_log_redir) |
| 226 |
{ |
{ |
| 227 |
log_common_redir(STDERR_FILENO); |
log_common_redir(STDERR_FILENO); |
| 228 |
} |
} |
| 229 |
if ((!daemon) && error_log_redir) |
if ((!daemon_service) && error_log_redir) |
| 230 |
{ |
{ |
| 231 |
log_error_redir(STDERR_FILENO); |
log_error_redir(STDERR_FILENO); |
| 232 |
} |
} |