Skip to content

Commit fbf43f9

Browse files
author
tymon
committed
add option of saving pid file
Signed-off-by: tymon <tymon@tymon-32bit.(none)>
1 parent b63ebe4 commit fbf43f9

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-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+
config->save_pid = 1;
171+
strncpy(config->pidfile, optarg, sizeof(config->pidfile));
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.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ typedef struct {
162162
char *internal_sock; /**< @brief internal path to socket */
163163
int deltatraffic; /**< @brief reset each user's traffic (Outgoing and Incoming) value after each Auth operation. */
164164
int daemon; /**< @brief if daemon > 0, use daemon mode */
165+
int save_pid; /**< @brief if save_pid == 1, save the pid in file */
166+
char pidfile[255]; /**< @brief pid file path of wifidog */
165167
char *external_interface; /**< @brief External network interface name for
166168
firewall rules */
167169
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->save_pid)
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,21 @@ 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, "%lu\n", getpid());
404+
fclose(f);
405+
}
406+
}
407+
408+
return;
409+
}

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 *pf);
63+
6164
#endif /* _UTIL_H_ */

0 commit comments

Comments
 (0)