chiark / gitweb /
volume_id: use shared library by default
[elogind.git] / extras / run_directory / run_directory.c
1 /*
2  * udev_run_directory.c - directory multiplexer
3  *
4  * Copyright (C) 2005 Kay Sievers <kay@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  *
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stddef.h>
16 #include <dirent.h>
17 #include <errno.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <limits.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <sys/stat.h>
24
25 #include "../../udev.h"
26 #include "run_directory.h"
27
28 static int exec_program(const char *filename, const char *subsystem)
29 {
30         pid_t pid;
31
32         dbg("running %s", filename);
33         pid = fork();
34         switch (pid) {
35         case 0:
36                 /* child */
37                 execl(filename, filename, subsystem, NULL);
38                 dbg("exec of child failed");
39                 _exit(1);
40         case -1:
41                 dbg("fork of child failed");
42                 break;
43                 return -1;
44         default:
45                 waitpid(pid, NULL, 0);
46         }
47
48         return 0;
49 }
50
51 int run_directory(const char *dir, const char *suffix, const char *subsystem)
52 {
53         struct name_entry *name_loop, *name_tmp;
54         struct stat buf;
55         LIST_HEAD(name_list);
56
57         dbg("looking at '%s'", dir);
58
59         if (stat(dir, &buf) != 0) {
60                 dbg("directory '%s' not found", dir);
61                 return 0;
62         }
63
64         add_matching_files(&name_list, dir, suffix);
65
66         list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
67                 exec_program(name_loop->name, subsystem);
68                 list_del(&name_loop->node);
69         }
70
71         logging_close();
72         return 0;
73 }