| 17 |
|
|
| 18 |
#include "bbs.h" |
#include "bbs.h" |
| 19 |
#include "common.h" |
#include "common.h" |
| 20 |
|
#include "io.h" |
| 21 |
|
#include "menu.h" |
| 22 |
|
#include <signal.h> |
| 23 |
|
#include <string.h> |
| 24 |
|
#include <sys/types.h> |
| 25 |
|
#include <unistd.h> |
| 26 |
|
|
| 27 |
|
void |
| 28 |
|
app_help (void) |
| 29 |
|
{ |
| 30 |
|
prints ("Usage: bbsd [-fhv] [...]\n\n" |
| 31 |
|
"-f\t--foreground\t\tForce program run in foreground\n" |
| 32 |
|
"-h\t--help\t\t\tDisplay this help message\n" |
| 33 |
|
"-v\t--version\t\tDisplay version information\n" |
| 34 |
|
"\t--display-log\t\tDisplay standard log information\n" |
| 35 |
|
"\t--display-error-log\tDisplay error log information\n" |
| 36 |
|
"\n If meet any bug, please report to <leaflet@leafok.com>\n\n"); |
| 37 |
|
} |
| 38 |
|
|
| 39 |
|
void |
| 40 |
|
arg_error (void) |
| 41 |
|
{ |
| 42 |
|
prints ("Invalid arguments\n"); |
| 43 |
|
app_help (); |
| 44 |
|
} |
| 45 |
|
|
| 46 |
int |
int |
| 47 |
main (int argc, char *argv[]) |
main (int argc, char *argv[]) |
| 48 |
{ |
{ |
| 49 |
char log_dir[256], file_log_std[256], file_log_error[256], file_config[256]; |
char log_dir[256], file_log_std[256], file_log_error[256], file_config[256]; |
| 50 |
|
int i, j; |
| 51 |
|
int daemon = 1, std_log_redir = 0, error_log_redir = 0; |
| 52 |
|
|
| 53 |
|
//Parse args |
| 54 |
|
for (i = 1; i < argc; i++) |
| 55 |
|
{ |
| 56 |
|
switch (argv[i][0]) |
| 57 |
|
{ |
| 58 |
|
case '-': |
| 59 |
|
if (argv[i][1] != '-') |
| 60 |
|
{ |
| 61 |
|
for (j = 1; j < strlen (argv[i]); j++) |
| 62 |
|
{ |
| 63 |
|
switch (argv[i][j]) |
| 64 |
|
{ |
| 65 |
|
case 'f': |
| 66 |
|
daemon = 0; |
| 67 |
|
break; |
| 68 |
|
case 'h': |
| 69 |
|
app_help (); |
| 70 |
|
exit (0); |
| 71 |
|
case 'v': |
| 72 |
|
puts (app_version); |
| 73 |
|
exit (0); |
| 74 |
|
default: |
| 75 |
|
arg_error (); |
| 76 |
|
exit (1); |
| 77 |
|
} |
| 78 |
|
} |
| 79 |
|
} |
| 80 |
|
else |
| 81 |
|
{ |
| 82 |
|
if (strcmp (argv[i] + 2, "foreground") == 0) |
| 83 |
|
{ |
| 84 |
|
daemon = 0; |
| 85 |
|
break; |
| 86 |
|
} |
| 87 |
|
if (strcmp (argv[i] + 2, "help") == 0) |
| 88 |
|
{ |
| 89 |
|
app_help (); |
| 90 |
|
exit (0); |
| 91 |
|
} |
| 92 |
|
if (strcmp (argv[i] + 2, "version") == 0) |
| 93 |
|
{ |
| 94 |
|
puts (app_version); |
| 95 |
|
exit (0); |
| 96 |
|
} |
| 97 |
|
if (strcmp (argv[i] + 2, "display-log") == 0) |
| 98 |
|
{ |
| 99 |
|
std_log_redir = 1; |
| 100 |
|
} |
| 101 |
|
if (strcmp (argv[i] + 2, "display-error-log") == 0) |
| 102 |
|
{ |
| 103 |
|
error_log_redir = 1; |
| 104 |
|
} |
| 105 |
|
} |
| 106 |
|
break; |
| 107 |
|
} |
| 108 |
|
} |
| 109 |
|
|
| 110 |
//Initialize daemon |
//Initialize daemon |
| 111 |
init_daemon (); |
if (daemon) |
| 112 |
|
init_daemon (); |
| 113 |
|
|
| 114 |
|
//Change current dir |
| 115 |
|
strncpy (app_home_dir, argv[0], rindex (argv[0], '/') - argv[0] + 1); |
| 116 |
|
strcat (app_home_dir, "../"); |
| 117 |
|
chdir (app_home_dir); |
| 118 |
|
|
| 119 |
//Initialize log |
//Initialize log |
| 120 |
strncpy(app_home_dir, argv[0], rindex(argv[0],'/')-argv[0]+1); |
strcpy (app_temp_dir, "/tmp/lbbs/"); |
| 121 |
strcpy(log_dir, app_home_dir); |
mkdir (app_temp_dir, 0750); |
| 122 |
strcat(log_dir, "log/"); |
strcpy (log_dir, app_home_dir); |
| 123 |
strcpy(file_log_std, log_dir); |
strcat (log_dir, "log/"); |
| 124 |
strcpy(file_log_error, log_dir); |
strcpy (file_log_std, log_dir); |
| 125 |
strcat(file_log_std, "bbsd.log"); |
strcpy (file_log_error, log_dir); |
| 126 |
strcat(file_log_error, "error.log"); |
strcat (file_log_std, "bbsd.log"); |
| 127 |
mkdir(log_dir,0700); |
strcat (file_log_error, "error.log"); |
| 128 |
if (log_begin(file_log_std,file_log_error)<0) |
mkdir (log_dir, 0750); |
| 129 |
exit(-1); |
if (log_begin (file_log_std, file_log_error) < 0) |
| 130 |
|
exit (-1); |
| 131 |
|
|
| 132 |
|
if ((!daemon) && std_log_redir) |
| 133 |
|
log_std_redirect (2); |
| 134 |
|
if ((!daemon) && error_log_redir) |
| 135 |
|
log_err_redirect (3); |
| 136 |
|
|
| 137 |
//Load configuration |
//Load configuration |
| 138 |
strcpy(file_config, app_home_dir); |
strcpy (file_config, app_home_dir); |
| 139 |
strcat(file_config, "conf/bbsd.conf"); |
strcat (file_config, "conf/bbsd.conf"); |
| 140 |
if (load_conf(file_config)<0) |
if (load_conf (file_config) < 0) |
| 141 |
exit(-2); |
exit (-2); |
| 142 |
|
|
| 143 |
|
//Load menus |
| 144 |
|
strcpy (file_config, app_home_dir); |
| 145 |
|
strcat (file_config, "conf/menu.conf"); |
| 146 |
|
if (load_menu (&bbs_menu, file_config) < 0) |
| 147 |
|
exit (-3); |
| 148 |
|
|
| 149 |
|
//Set signal handler |
| 150 |
|
signal (SIGCHLD, child_exit); |
| 151 |
|
signal (SIGTERM, system_exit); |
| 152 |
|
signal (SIG_RELOAD_MENU, reload_bbs_menu); |
| 153 |
|
|
| 154 |
//Initialize socket server |
//Initialize socket server |
| 155 |
net_server(BBS_address, BBS_port); |
net_server (BBS_address, BBS_port); |
| 156 |
|
|
| 157 |
/* |
//Wait for child process exit |
| 158 |
FILE *fp; |
while (SYS_child_process_count > 0) |
| 159 |
time_t t; |
{ |
| 160 |
|
log_std ("."); |
| 161 |
while (1) |
sleep(1); |
| 162 |
{ |
} |
|
sleep (60); |
|
|
if ((fp = fopen ("bbsd.log", "a")) >= 0) |
|
|
{ |
|
|
t = time (0); |
|
|
fprintf (fp, "I'm here at %s\n", asctime (localtime (&t))); |
|
|
fclose (fp); |
|
|
} |
|
|
} |
|
|
*/ |
|
| 163 |
|
|
| 164 |
return 0; |
return 0; |
| 165 |
} |
} |