add option pid

This commit is contained in:
Nick Peng
2018-06-20 23:41:47 +08:00
parent 2fec5f7e14
commit 0e72262be3
2 changed files with 9 additions and 3 deletions

View File

@@ -51,6 +51,7 @@ void help(void)
"Start smartdns server.\n" "Start smartdns server.\n"
" -f run forground.\n" " -f run forground.\n"
" -c [conf] config file.\n" " -c [conf] config file.\n"
" -p [pid] pid file path\n"
" -h show this help message.\n" " -h show this help message.\n"
"\n"; "\n";
/* clang-format on */ /* clang-format on */
@@ -251,10 +252,12 @@ int main(int argc, char *argv[])
int is_forground = 0; int is_forground = 0;
int opt; int opt;
char config_file[MAX_LINE_LEN]; char config_file[MAX_LINE_LEN];
char pid_file[MAX_LINE_LEN];
strncpy(config_file, SMARTDNS_CONF_FILE, MAX_LINE_LEN); strncpy(config_file, SMARTDNS_CONF_FILE, MAX_LINE_LEN);
strncpy(pid_file, SMARTDNS_PID_FILE, MAX_LINE_LEN);
while ((opt = getopt(argc, argv, "fhc:")) != -1) { while ((opt = getopt(argc, argv, "fhc:p:")) != -1) {
switch (opt) { switch (opt) {
case 'f': case 'f':
is_forground = 1; is_forground = 1;
@@ -262,6 +265,9 @@ int main(int argc, char *argv[])
case 'c': case 'c':
snprintf(config_file, sizeof(config_file), optarg); snprintf(config_file, sizeof(config_file), optarg);
break; break;
case 'p':
snprintf(pid_file, sizeof(pid_file), optarg);
break;
case 'h': case 'h':
help(); help();
return 1; return 1;
@@ -280,7 +286,7 @@ int main(int argc, char *argv[])
if (load_conf(config_file) != 0) { if (load_conf(config_file) != 0) {
} }
if (create_pid_file(SMARTDNS_PID_FILE) != 0) { if (create_pid_file(pid_file) != 0) {
fprintf(stderr, "create pid file failed, %s\n", strerror(errno)); fprintf(stderr, "create pid file failed, %s\n", strerror(errno));
goto errout; goto errout;
} }

View File

@@ -4,7 +4,7 @@ After=network.target
[Service] [Service]
PIDFile=/var/run/smartdns.pid PIDFile=/var/run/smartdns.pid
EnvironmentFile=-/etc/default/smartdns EnvironmentFile=/etc/default/smartdns
ExecStart=/usr/sbin/smartdns $SMART_DNS_OPTS ExecStart=/usr/sbin/smartdns $SMART_DNS_OPTS
KillMode=process KillMode=process
Restart=always Restart=always