aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremias Stotter <jeremias@stotter.eu>2021-12-01 22:33:34 +0100
committerJeremias Stotter <jeremias@stotter.eu>2021-12-01 22:33:34 +0100
commit787fbeaf737d7f45763836e8d5cfa8d562cda848 (patch)
treeabd706e87e26c6cbcb3488c818698f2c7adde4c7
parent61e0a20a0506575fa8f0d459d8eec40a8c59bafa (diff)
downloadJBlog-787fbeaf737d7f45763836e8d5cfa8d562cda848.tar.gz
JBlog-787fbeaf737d7f45763836e8d5cfa8d562cda848.tar.bz2
JBlog-787fbeaf737d7f45763836e8d5cfa8d562cda848.zip
PID file is now human readable
-rw-r--r--jblog.c23
1 files changed, 13 insertions, 10 deletions
diff --git a/jblog.c b/jblog.c
index 20a14fa..f62bf91 100644
--- a/jblog.c
+++ b/jblog.c
@@ -195,7 +195,7 @@ int main(int argc, char* argv[]) {
" [-g <group>] group the process will setgid to\n"
" [-d] daemonize the process\n"
" [-l <logfile>] log file\n"
- " [-r <pidfile>] the path of the PID file (defaults to "PIDFILE", only usefull with -d\n"
+ " [-r <pidfile>] the path of the PID file (defaults to "PIDFILE", only usefull with -d)\n"
" [-h] display this dialog, then exit\n");
return 0;
break;
@@ -261,9 +261,14 @@ int main(int argc, char* argv[]) {
return -1;
}
} else {
+ char read_pid[64] = "";
pid_t existing_daemon = 0;
- if(read(pidfile, &existing_daemon, sizeof(pid_t)) != sizeof(pid_t)) {
- jb_log(LL_ERR, true, "Can't reading PID file");
+ if(read(pidfile, read_pid, 64) < 1 ) {
+ jb_log(LL_ERR, true, "reading PID file");
+ return -1;
+ }
+ if(sscanf(read_pid, "%d", &existing_daemon) != 1) {
+ jb_log(LL_ERR, false, "PID file seems to be invalid, try killing jblog and removing it by hand");
return -1;
}
if(kill(existing_daemon, 0) == 0 || errno != ESRCH) {
@@ -313,7 +318,7 @@ int main(int argc, char* argv[]) {
while(child_signal != closeparent_sig) {
(void)!read(parentpipe[0], &child_signal, 1);
if(child_signal == errorparent_sig) {
- jb_log(LL_ERR, false, "couldn't start child, look at the log file if it is set");
+ jb_log(LL_ERR, false, "couldn't start child, look at the log file (you can specify a log file with the -l option)");
return -1;
}
}
@@ -363,16 +368,12 @@ int main(int argc, char* argv[]) {
return -1;
}
pid_t daemon_pid = getpid();
- if(write(pidfile, &daemon_pid, sizeof(daemon_pid)) != sizeof(daemon_pid)) {
+ if(dprintf(pidfile, "%d\n", daemon_pid) < 1) {
jb_log(LL_ERR, true, "writing pidfile");
(void)!write(parentpipe[1], &errorparent_sig, 1);
return -1;
}
close(pidfile);
- // Signal the parent to stop
- // @todo only do this once the main loop is actually started
- (void)!write(parentpipe[1], &closeparent_sig, 1);
- close(parentpipe[1]);
}
}
// Now we are a daemon
@@ -507,8 +508,10 @@ int main(int argc, char* argv[]) {
}
// Tell the parent we are ready to start the main loop
- if(daemon)
+ if(daemon) {
(void)!write(parentpipe[1], &closeparent_sig, 1);
+ close(parentpipe[1]);
+ }
// Start the main loop
for(;;) {
int connected_sock = accept(sockfd, (struct sockaddr *)&socket_address, &un_socklen);
Jeremias Stotters git repositories generated by CGIT