Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option of saving pid file #217

Merged
merged 7 commits into from
Aug 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/commandline.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ usage(void)
" -x pid Used internally by WiFiDog when re-starting itself *DO NOT ISSUE THIS SWITCH MANUAlLY*\n");
fprintf(stdout, " -i <path> Internal socket path used when re-starting self\n");
fprintf(stdout, " -a <path> Path to /proc/net/arp replacement - mainly useful for debugging.\n");
fprintf(stdout, " -p <path> Save pid to file\n");
fprintf(stdout, "\n");
}

Expand Down Expand Up @@ -164,6 +165,15 @@ parse_commandline(int argc, char **argv)
exit(1);
}
break;
case 'p':
if (optarg) {
free(config->pidfile);
config->pidfile = safe_strdup(optarg);
} else {
fprintf(stdout, "The expected PID file path to the wifidog was not supplied!\n");
exit(1);
}
break;
default:
usage();
exit(1);
Expand Down
1 change: 1 addition & 0 deletions src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ config_init(void)
config.clienttimeout = DEFAULT_CLIENTTIMEOUT;
config.checkinterval = DEFAULT_CHECKINTERVAL;
config.daemon = -1;
config.pidfile = NULL;
config.wdctl_sock = safe_strdup(DEFAULT_WDCTL_SOCK);
config.internal_sock = safe_strdup(DEFAULT_INTERNAL_SOCK);
config.rulesets = NULL;
Expand Down
1 change: 1 addition & 0 deletions src/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ typedef struct {
char *internal_sock; /**< @brief internal path to socket */
int deltatraffic; /**< @brief reset each user's traffic (Outgoing and Incoming) value after each Auth operation. */
int daemon; /**< @brief if daemon > 0, use daemon mode */
char *pidfile; /**< @brief pid file path of wifidog */
char *external_interface; /**< @brief External network interface name for
firewall rules */
char *gw_id; /**< @brief ID of the Gateway, sent to central
Expand Down
4 changes: 4 additions & 0 deletions src/gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ main_loop(void)
started_time = time(NULL);
}

/* save the pid file if needed */
if ((!config) && (!config->pidfile))
save_pid_file(config->pidfile);

/* If we don't have the Gateway IP address, get it. Can't fail. */
if (!config->gw_address) {
debug(LOG_DEBUG, "Finding IP address of %s", config->gw_interface);
Expand Down
22 changes: 22 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,25 @@ rand16(void)
* ignore that one. */
return ((unsigned short)(rand() >> 15));
}

/*
* Save pid of this wifidog in pid file
* @param 'pf' as string, it is the pid file absolutely path
*/
void
save_pid_file(const char *pf)
{
if (pf) {
FILE *f = fopen(pf, "w");
if (f) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide a meaningful error message to the user if the fopen call fails?

fprintf(f, "%d\n", getpid());

int ret = fclose(f);
if (ret == EOF) /* check the return value of fclose */
debug(LOG_ERR, "fclose() on file %s was failed (%s)", pf, strerror(errno));
} else /* fopen return NULL, open file failed */
debug(LOG_ERR, "fopen() on flie %s was failed (%s)", pf, strerror(errno));
}

return;
}
3 changes: 3 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,7 @@ void close_icmp_socket(void);
/** @brief ICMP Ping an IP */
void icmp_ping(const char *);

/** @brief Save pid of this wifidog in pid file */
void save_pid_file(const char *);

#endif /* _UTIL_H_ */