-
Notifications
You must be signed in to change notification settings - Fork 2
/
daemon.c
189 lines (161 loc) · 4.85 KB
/
daemon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
Copyright (C) 2021 Michel Stempin <[email protected]>
This file is part of the FunKey S GPIO keyboard daemon.
This is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
*/
/**
* @file daemon.c
* This file contains the daemon function for the FunKey S GPIO daemon
*/
#include <stdio.h>
#include <signal.h>
#include <syslog.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "daemon.h"
/* Process ID file descriptor */
static int pid_fd;
/* Process ID lock file name */
static char *pid_lock_file;
/* Signal handler */
void signal_handler(int sig)
{
switch(sig){
case SIGHUP:
syslog(LOG_WARNING, "Received SIGHUP.");
break;
case SIGINT:
case SIGTERM:
syslog(LOG_INFO, "Exiting.");
close(pid_fd);
unlink(pid_lock_file);
exit(EXIT_SUCCESS);
break;
default:
syslog(LOG_WARNING, "Unhandled signal %s", strsignal(sig));
}
}
/* Get the pid from the pid lock file and terminate the daemon */
void kill_daemon(char *pidfile)
{
int n;
pid_t pid;
char str[10];
pid_fd = open(pidfile, O_RDONLY, 0600);
if (pid_fd < 0) {
perror(pidfile);
} else {
if (read(pid_fd, str, 10) > 0) {
pid = strtol(str, NULL, 0);
if (pid) {
printf("terminating %d\n", pid);
n = kill(pid, SIGTERM);
if (n < 0) {
perror("kill");
}
}
}
close(pid_fd);
}
}
/* Turn the running process into a daemon */
void daemonize(const char *ident, char *rundir, char *pidfile)
{
int pid, i;
char str[10];
struct sigaction sa;
sigset_t ss;
/* Check if parent process is already init */
if (getppid() == 1) {
/* Don't need to do it twice */
return;
}
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
perror("first fork");
exit(EXIT_FAILURE);
} else if (pid > 0) {
/* Success. terminate the parent */
exit(EXIT_SUCCESS);
}
/* On success: the child process becomes the session leader */
if (setsid() < 0) {
perror("set SID");
exit(EXIT_FAILURE);
}
/* Block these signals */
sigemptyset(&ss);
sigaddset(&ss, SIGCHLD);
sigaddset(&ss, SIGTSTP);
sigaddset(&ss, SIGTTOU);
sigaddset(&ss, SIGTTIN);
sigprocmask(SIG_BLOCK, &ss, NULL);
/* Handle these signals */
sa.sa_handler = signal_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
/* Fork off for a second time to ensure that we get rid of the session
* leading process
*/
pid = fork();
if (pid < 0) {
perror("second fork");
exit(EXIT_FAILURE);
} else if (pid > 0) {
/* Success. terminate the parent */
exit(EXIT_SUCCESS);
}
/* Set new file permissions */
umask(0);
/* Change the working directory to the root directory or another
* appropriated directory
*/
if (chdir(rundir) < 0) {
perror(rundir);
}
/* Close all opened file descriptors */
for (i = sysconf(_SC_OPEN_MAX); i >= 0; i--) {
close(i);
}
/* Since we have no more stdout/stderr, open syslog */
openlog(ident, LOG_PID | LOG_NDELAY, LOG_DAEMON);
/* Make sure there is only one daemon running at a time using a lock file */
pid_lock_file = pidfile;
pid_fd = open(pidfile, O_RDWR | O_CREAT, 0600);
if (pid_fd < 0) {
syslog(LOG_INFO, "Could not open lock file %s. Exiting.", pidfile);
exit(EXIT_FAILURE);
}
if (lockf(pid_fd, F_TLOCK, 0) < 0) {
syslog(LOG_INFO, "Could not lock lock file %s. Exiting.", pidfile);
unlink(pidfile);
exit(EXIT_FAILURE);
}
sprintf(str, "%d\n", getpid());
if (write(pid_fd, str, strlen(str)) < 0) {
syslog(LOG_ERR, "Could not write to lock file %s: %s. Exiting.",
strerror(errno), pidfile);
unlink(pidfile);
exit(EXIT_FAILURE);
}
syslog(LOG_INFO, "Daemon running.");
}