Skip to content

Commit ed6aed8

Browse files
committed
Merge pull request #217 from hbl0307106015/master
add option of saving pid file
2 parents e87dfe3 + acb74c5 commit ed6aed8

File tree

6 files changed

+41
-0
lines changed

6 files changed

+41
-0
lines changed

src/commandline.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ usage(void)
7272
" -x pid Used internally by WiFiDog when re-starting itself *DO NOT ISSUE THIS SWITCH MANUAlLY*\n");
7373
fprintf(stdout, " -i <path> Internal socket path used when re-starting self\n");
7474
fprintf(stdout, " -a <path> Path to /proc/net/arp replacement - mainly useful for debugging.\n");
75+
fprintf(stdout, " -p <path> Save pid to file\n");
7576
fprintf(stdout, "\n");
7677
}
7778

@@ -164,6 +165,15 @@ parse_commandline(int argc, char **argv)
164165
exit(1);
165166
}
166167
break;
168+
case 'p':
169+
if (optarg) {
170+
free(config->pidfile);
171+
config->pidfile = safe_strdup(optarg);
172+
} else {
173+
fprintf(stdout, "The expected PID file path to the wifidog was not supplied!\n");
174+
exit(1);
175+
}
176+
break;
167177
default:
168178
usage();
169179
exit(1);

src/conf.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ config_init(void)
195195
config.clienttimeout = DEFAULT_CLIENTTIMEOUT;
196196
config.checkinterval = DEFAULT_CHECKINTERVAL;
197197
config.daemon = -1;
198+
config.pidfile = NULL;
198199
config.wdctl_sock = safe_strdup(DEFAULT_WDCTL_SOCK);
199200
config.internal_sock = safe_strdup(DEFAULT_INTERNAL_SOCK);
200201
config.rulesets = NULL;

src/conf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ typedef struct {
163163
char *internal_sock; /**< @brief internal path to socket */
164164
int deltatraffic; /**< @brief reset each user's traffic (Outgoing and Incoming) value after each Auth operation. */
165165
int daemon; /**< @brief if daemon > 0, use daemon mode */
166+
char *pidfile; /**< @brief pid file path of wifidog */
166167
char *external_interface; /**< @brief External network interface name for
167168
firewall rules */
168169
char *gw_id; /**< @brief ID of the Gateway, sent to central

src/gateway.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ main_loop(void)
366366
started_time = time(NULL);
367367
}
368368

369+
/* save the pid file if needed */
370+
if ((!config) && (!config->pidfile))
371+
save_pid_file(config->pidfile);
372+
369373
/* If we don't have the Gateway IP address, get it. Can't fail. */
370374
if (!config->gw_address) {
371375
debug(LOG_DEBUG, "Finding IP address of %s", config->gw_interface);

src/util.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,25 @@ rand16(void)
389389
* ignore that one. */
390390
return ((unsigned short)(rand() >> 15));
391391
}
392+
393+
/*
394+
* Save pid of this wifidog in pid file
395+
* @param 'pf' as string, it is the pid file absolutely path
396+
*/
397+
void
398+
save_pid_file(const char *pf)
399+
{
400+
if (pf) {
401+
FILE *f = fopen(pf, "w");
402+
if (f) {
403+
fprintf(f, "%d\n", getpid());
404+
405+
int ret = fclose(f);
406+
if (ret == EOF) /* check the return value of fclose */
407+
debug(LOG_ERR, "fclose() on file %s was failed (%s)", pf, strerror(errno));
408+
} else /* fopen return NULL, open file failed */
409+
debug(LOG_ERR, "fopen() on flie %s was failed (%s)", pf, strerror(errno));
410+
}
411+
412+
return;
413+
}

src/util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,7 @@ void close_icmp_socket(void);
5858
/** @brief ICMP Ping an IP */
5959
void icmp_ping(const char *);
6060

61+
/** @brief Save pid of this wifidog in pid file */
62+
void save_pid_file(const char *);
63+
6164
#endif /* _UTIL_H_ */

0 commit comments

Comments
 (0)