fix systemd unit install path
[external/acpid.git] / acpid.c
1 /*
2  *  acpid.c - ACPI daemon
3  *
4  *  Portions Copyright (C) 2000 Andrew Henroid
5  *  Portions Copyright (C) 2001 Sun Microsystems
6  *  Portions Copyright (C) 2004 Tim Hockin (thockin@hockin.org)
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <getopt.h>
33
34 #include "acpid.h"
35 #include "log.h"
36 #include "event.h"
37 #include "connection_list.h"
38 #include "proc.h"
39 #include "sock.h"
40 #include "input_layer.h"
41 #include "inotify_handler.h"
42 #include "netlink.h"
43
44 static int handle_cmdline(int *argc, char ***argv);
45 static void close_fds(void);
46 static int daemonize(void);
47 static void open_log(void);
48 static int std2null(void);
49 static int create_pidfile(void);
50 static void clean_exit(int sig);
51 static void reload_conf(int sig);
52
53 /* global debug level */
54 int acpid_debug;
55
56 /* do we log event info? */
57 int logevents;
58
59 const char *progname;
60 static const char *confdir = ACPID_CONFDIR;
61 static const char *lockfile = ACPID_LOCKFILE;
62 static int nosocket;
63 static int foreground;
64 static const char *pidfile = ACPID_PIDFILE;
65 static int netlink;
66
67 int
68 main(int argc, char **argv)
69 {
70         /* learn who we really are */
71         progname = (const char *)strrchr(argv[0], '/');
72         progname = progname ? (progname + 1) : argv[0];
73
74         /* handle the commandline  */
75         handle_cmdline(&argc, &argv);
76
77         /* close any extra file descriptors */
78         close_fds();
79
80         /* open the log */
81         open_log();
82         
83         if (!netlink) {
84                 /* open the acpi event file in the proc fs */
85                 /* if the open fails, try netlink */
86                 if (open_proc())
87                         netlink = 1;
88         }
89
90         if (netlink) {
91                 /* open the input layer */
92                 open_input();
93
94                 /* watch for new input layer devices */
95                 open_inotify();
96
97                 /* open netlink */
98                 open_netlink();
99         }
100
101         /* open our socket */
102         if (!nosocket) {
103                 open_sock();
104         }
105
106         /* if we're running in the background, and we're not being started */
107         /* by systemd */
108         if (!foreground  &&  !is_socket(STDIN_FILENO)) {
109                 if (daemonize() < 0)
110                         exit(EXIT_FAILURE);
111         }
112
113         /* redirect standard files to /dev/null */
114         if (std2null() < 0) {
115                 exit(EXIT_FAILURE);
116         }
117         
118         acpid_log(LOG_INFO, "starting up with %s",
119                 netlink ? "netlink and the input layer" : "proc fs");
120
121         /* trap key signals */
122         signal(SIGHUP, reload_conf);
123         signal(SIGINT, clean_exit);
124         signal(SIGQUIT, clean_exit);
125         signal(SIGTERM, clean_exit);
126         signal(SIGPIPE, SIG_IGN);
127
128         /* read in our configuration */
129         if (acpid_read_conf(confdir)) {
130                 exit(EXIT_FAILURE);
131         }
132
133         /* create our pidfile */
134         if (create_pidfile() < 0) {
135                 exit(EXIT_FAILURE);
136         }
137
138         acpid_log(LOG_INFO, "waiting for events: event logging is %s",
139             logevents ? "on" : "off");
140
141         /* main loop */
142         while (1) {
143                 fd_set readfds;
144                 int nready;
145                 int i;
146                 struct connection *p;
147
148                 /* it's going to get clobbered, so use a copy */
149                 readfds = *get_fdset();
150
151                 /* wait on data */
152                 nready = select(get_highestfd() + 1, &readfds, NULL, NULL, NULL);
153
154                 if (nready < 0  &&  errno == EINTR) {
155                         continue;
156                 } else if (nready < 0) {
157                         acpid_log(LOG_ERR, "select(): %s", strerror(errno));
158                         continue;
159                 }
160
161                 /* house keeping */
162                 acpid_close_dead_clients();
163
164                 /* for each connection */
165                 for (i = 0; i <= get_number_of_connections(); ++i) {
166                         int fd;
167
168                         p = get_connection(i);
169
170                         /* if this connection is invalid, bail */
171                         if (!p)
172                                 break;
173
174                         /* get the file descriptor */
175                         fd = p->fd;
176
177                         /* if this file descriptor has data waiting */
178                         if (FD_ISSET(fd, &readfds)) {
179                                 /* delegate to this connection's process function */
180                                 p->process(fd);
181                         }
182                 }
183         }
184
185         clean_exit_with_status(EXIT_SUCCESS);
186
187         return 0;
188 }
189
190 /*
191  * Parse command line arguments
192  */
193 static int
194 handle_cmdline(int *argc, char ***argv)
195 {
196         struct option opts[] = {
197                 {"confdir", 1, 0, 'c'},
198                 {"clientmax", 1, 0, 'C'},
199                 {"debug", 0, 0, 'd'},
200                 {"eventfile", 1, 0, 'e'},
201                 {"foreground", 0, 0, 'f'},
202                 {"logevents", 0, 0, 'l'},
203                 {"socketgroup", 1, 0, 'g'},
204                 {"socketmode", 1, 0, 'm'},
205                 {"socketfile", 1, 0, 's'},
206                 {"nosocket", 1, 0, 'S'},
207                 {"pidfile", 1, 0, 'p'},
208                 {"lockfile", 1, 0, 'L'},
209                 {"netlink", 0, 0, 'n'},
210                 {"version", 0, 0, 'v'},
211                 {"help", 0, 0, 'h'},
212                 {NULL, 0, 0, 0},
213         };
214         const char *opts_help[] = {
215                 "Set the configuration directory.",     /* confdir */
216                 "Set the limit on non-root socket connections.",/* clientmax */
217                 "Increase debugging level (implies -f).",/* debug */
218                 "Use the specified file for events.",   /* eventfile */
219                 "Run in the foreground.",               /* foreground */
220                 "Log all event activity.",              /* logevents */
221                 "Set the group on the socket file.",    /* socketgroup */
222                 "Set the permissions on the socket file.",/* socketmode */
223                 "Use the specified socket file.",       /* socketfile */
224                 "Do not listen on a UNIX socket (overrides -s).",/* nosocket */
225                 "Use the specified PID file.",          /* pidfile */
226                 "Use the specified lockfile to stop processing.", /* lockfile */
227                 "Force netlink/input layer mode. (overrides -e)", /* netlink */
228                 "Print version information.",           /* version */
229                 "Print this message.",                  /* help */
230         };
231         struct option *opt;
232         const char **hlp;
233         int max, size;
234
235         for (;;) {
236                 int i;
237                 i = getopt_long(*argc, *argv,
238                     "c:C:de:flg:m:s:Sp:L:nvh", opts, NULL);
239                 if (i == -1) {
240                         break;
241                 }
242                 switch (i) {
243                 case 'c':
244                         confdir = optarg;
245                         break;
246                 case 'C':
247                         clientmax = strtol(optarg, NULL, 0);
248                         break;
249                 case 'd':
250                         foreground = 1;
251                         acpid_debug++;
252             log_debug_to_stderr = 1;
253                         break;
254                 case 'e':
255                         eventfile = optarg;
256                         break;
257                 case 'f':
258                         foreground = 1;
259                         break;
260                 case 'l':
261                         logevents = 1;
262                         break;
263                 case 'g':
264                         socketgroup = optarg;
265                         break;
266                 case 'm':
267                         socketmode = strtol(optarg, NULL, 8);
268                         break;
269                 case 's':
270                         socketfile = optarg;
271                         break;
272                 case 'S':
273                         nosocket = 1;
274                         break;
275                 case 'p':
276                         pidfile = optarg;
277                         break;
278                 case 'L':
279                         lockfile = optarg;
280                         break;
281                 case 'n':
282                         netlink = 1;
283                         break;
284                 case 'v':
285                         printf(PACKAGE "-" VERSION "\n");
286                         exit(EXIT_SUCCESS);
287                 case 'h':
288                 default:
289                         fprintf(stderr, "Usage: %s [OPTIONS]\n", progname);
290                         max = 0;
291                         for (opt = opts; opt->name; opt++) {
292                                 size = strlen(opt->name);
293                                 if (size > max)
294                                         max = size;
295                         }
296                         for (opt = opts, hlp = opts_help;
297                              opt->name;
298                              opt++, hlp++) {
299                                 fprintf(stderr, "  -%c, --%s",
300                                         opt->val, opt->name);
301                                 size = strlen(opt->name);
302                                 for (; size < max; size++)
303                                         fprintf(stderr, " ");
304                                 fprintf(stderr, "  %s\n", *hlp);
305                         }
306                         exit(EXIT_FAILURE);
307                         break;
308                 }
309         }
310
311         *argc -= optind;
312         *argv += optind;
313
314         return 0;
315 }
316
317 static void
318 close_fds(void)
319 {
320         int fd, max;
321         max = sysconf(_SC_OPEN_MAX);
322         for (fd = 3; fd < max; fd++)
323                 close(fd);
324 }
325
326 static int
327 daemonize(void)
328 {
329         pid_t pid, sid;
330
331         /* fork off the parent process */
332         pid = fork();
333         if (pid < 0) {
334                 acpid_log(LOG_ERR, "fork: %s", strerror(errno));
335                 return -1;
336         }
337         /* if we got a good PID, then we can exit the parent process */
338         if (pid > 0) {
339                 exit(EXIT_SUCCESS);
340         }
341
342         /* at this point we are executing as the child process */
343
344         /* change the umask to something predictable instead of inheriting */
345         /* whatever from the parent */
346         umask(0);
347
348         /* create a new SID for the child process and */
349         /* detach the process from the parent (normally a shell) */
350         sid = setsid();
351         if (sid < 0) {
352                 acpid_log(LOG_ERR, "setsid: %s", strerror(errno));
353                 return -1;
354         }
355
356     /* Change the current working directory.  This prevents the current
357        directory from being locked; hence not being able to remove it. */
358         if (chdir("/") < 0) {
359                 acpid_log(LOG_ERR, "chdir(\"/\"): %s", strerror(errno));
360                 return -1;
361         }
362
363         return 0;
364 }
365
366 static void
367 open_log(void)
368 {
369         int log_opts;
370
371         /* open the syslog */
372         log_opts = LOG_CONS|LOG_NDELAY;
373         if (acpid_debug) {
374                 log_opts |= LOG_PERROR;
375         }
376         openlog(PACKAGE, log_opts, LOG_DAEMON);
377 }
378
379 static int
380 std2null(void)
381 {
382         int nullfd;
383
384         /* open /dev/null */
385         nullfd = open("/dev/null", O_RDWR);
386         if (nullfd < 0) {
387                 acpid_log(LOG_ERR, "can't open /dev/null: %s", strerror(errno));
388                 return -1;
389         }
390
391         /* set up stdin, stdout, stderr to /dev/null */
392
393         /* don't redirect stdin if we're being sent a socket by systemd */
394         if (!is_socket(STDIN_FILENO)  && 
395                         dup2(nullfd, STDIN_FILENO) != STDIN_FILENO) {
396                 acpid_log(LOG_ERR, "dup2() stdin: %s", strerror(errno));
397                 return -1;
398         }
399         if (!acpid_debug && dup2(nullfd, STDOUT_FILENO) != STDOUT_FILENO) {
400                 acpid_log(LOG_ERR, "dup2() stdout: %s", strerror(errno));
401                 return -1;
402         }
403         if (!acpid_debug && dup2(nullfd, STDERR_FILENO) != STDERR_FILENO) {
404                 acpid_log(LOG_ERR, "dup2() stderr: %s", strerror(errno));
405                 return -1;
406         }
407
408         close(nullfd);
409
410         return 0;
411 }
412
413 static int
414 create_pidfile(void)
415 {
416         int fd;
417
418         /* JIC */
419         unlink(pidfile);
420
421         /* open the pidfile */
422         fd = open(pidfile, O_WRONLY|O_CREAT|O_EXCL, 0644);
423         if (fd >= 0) {
424                 FILE *f;
425
426                 /* write our pid to it */
427                 f = fdopen(fd, "w");
428                 if (f != NULL) {
429                         fprintf(f, "%d\n", getpid());
430                         fclose(f);
431                         /* leave the fd open */
432                         return 0;
433                 }
434                 close(fd);
435         }
436
437         /* something went wrong */
438         acpid_log(LOG_ERR, "can't create pidfile %s: %s",
439                     pidfile, strerror(errno));
440         return -1;
441 }
442
443 void
444 clean_exit_with_status(int status)
445 {
446         acpid_cleanup_rules(1);
447         acpid_log(LOG_NOTICE, "exiting");
448         unlink(pidfile);
449         exit(status);
450 }
451
452 static void
453 clean_exit(int sig __attribute__((unused)))
454 {
455         clean_exit_with_status(EXIT_SUCCESS);
456 }
457
458 static void
459 reload_conf(int sig __attribute__((unused)))
460 {
461         acpid_log(LOG_NOTICE, "reloading configuration");
462         acpid_cleanup_rules(0);
463         acpid_read_conf(confdir);
464 }
465
466 int
467 locked()
468 {
469         struct stat trash;
470
471         /* check for existence of a lockfile */
472         return (stat(lockfile, &trash) == 0);
473 }
474