Lock the pid file.
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
int pidfile_open(const char *name)
{
+ struct flock lock;
+ p_clear(&lock, 1);
+ lock.l_type = F_WRLCK;
if (name) {
pidfile = fopen(name, "w");
if (!pidfile)
return -1;
- fprintf(pidfile, "%d\n", getpid());
+ if (fcntl(fileno(pidfile), F_SETLK, &lock) == -1) {
+ syslog(LOG_ERR, "program already started");
+ fclose(pidfile);
+ pidfile = NULL;
+ return -1;
+ }
+ fprintf(pidfile, "%d\n", getpid());
return fflush(pidfile);
}
return 0;
static void pidfile_close(void)
{
+ struct flock lock;
+ p_clear(&lock, 1);
+ lock.l_type = F_UNLCK;
if (pidfile) {
rewind(pidfile);
ftruncate(fileno(pidfile), 0);
- fclose(pidfile);
+ fcntl(fileno(pidfile), F_SETLK, &lock);
+ fclose(pidfile);
pidfile = NULL;
}
}
}
int server_loop(start_client_t starter, delete_client_t deleter,
- run_client_t runner, void* config) {
+ run_client_t runner, refresh_t refresh, void* config) {
while (!sigint) {
struct epoll_event evts[1024];
int n;
+ if (sighup && refresh) {
+ if (!refresh(config)) {
+ syslog(LOG_ERR, "error while refreshing configuration");
+ return EXIT_FAILURE;
+ }
+ }
+
n = epoll_select(evts, countof(evts), -1);
if (n < 0) {
if (errno != EAGAIN && errno != EINTR) {
typedef void (*delete_client_t)(void*);
typedef void *(*start_client_t)(server_t*);
typedef int (*run_client_t)(server_t*, void*);
+typedef bool (*refresh_t)(void*);
struct server_t {
unsigned listener : 1;
int start_server(int port, start_listener_t starter, delete_client_t deleter);
int server_loop(start_client_t starter, delete_client_t deleter,
- run_client_t runner, void* config);
+ run_client_t runner, refresh_t refresh, void* config);
#endif
|| start_listener(port_dec, true) < 0) {
return EXIT_FAILURE;
}
- return server_loop(srsd_starter, NULL, process_srs, &config);
+ return server_loop(srsd_starter, NULL, process_srs, NULL, &config);
}
}
{
int res = server_loop(query_starter, (delete_client_t)query_delete,
- policy_run, config);
+ policy_run, NULL, config);
config_delete(&config);
return res;
}