]> git.ipfire.org Git - thirdparty/systemd.git/blob - udeveventrecorder.c
[PATCH] Udev doesn't properly build with $CROSS
[thirdparty/systemd.git] / udeveventrecorder.c
1 /*
2 * udeveventrecorder.c
3 *
4 * Userspace devfs
5 *
6 * Copyright (C) 2004, 2005 Olaf Hering <olh@suse.de>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31
32 #include "udev.h"
33 #include "udev_version.h"
34 #include "udev_utils.h"
35 #include "logging.h"
36
37 #define BUFSIZE 12345
38 #define FNSIZE 123
39
40 static int log = 0;
41
42 #ifdef USE_LOG
43 void log_message (int priority, const char *format, ...)
44 {
45 va_list args;
46
47 if (priority > log)
48 return;
49
50 va_start(args, format);
51 vsyslog(priority, format, args);
52 va_end(args);
53 }
54 #endif
55
56 int main(int argc, char **argv, char **envp)
57 {
58 int fd, i;
59 unsigned long seq;
60 char **ep = envp;
61 char *buf, *p, *a;
62 struct stat sb;
63 const char *env;
64
65 if (stat("/events", &sb) || !(S_ISDIR(sb.st_mode)))
66 return 1;
67
68 env = getenv("UDEV_LOG");
69 if (env)
70 log = log_priority(env);
71
72 logging_init("udeveventrecorder");
73 dbg("version %s", UDEV_VERSION);
74
75 p = getenv("SEQNUM");
76 a = getenv("ACTION");
77 buf = malloc(FNSIZE);
78 if (!(buf && a && argv[1]))
79 goto error;
80 if (p)
81 seq = strtoul(p, NULL, 0);
82 else
83 seq = 0;
84
85 snprintf(buf, FNSIZE, "/events/debug.%05lu.%s.%s.%u", seq, argv[1], a ? a : "", getpid());
86 if ((fd = open(buf, O_CREAT | O_WRONLY | O_TRUNC, 0644)) < 0) {
87 err("error creating '%s'", buf);
88 goto error;
89 }
90 free(buf);
91 p = malloc(BUFSIZE);
92 buf = p;
93 buf += snprintf(buf, p + BUFSIZE - buf, "set --");
94 for (i = 1; i < argc; ++i) {
95 buf += snprintf(buf, p + BUFSIZE - buf, " %s", argv[i]);
96 if (buf > p + BUFSIZE)
97 goto full;
98 }
99 buf += snprintf(buf, p + BUFSIZE - buf, "\n");
100 if (buf > p + BUFSIZE)
101 goto full;
102 while (*ep) {
103 unsigned char *t;
104 t = memchr(*ep, '=', strlen(*ep));
105 if (t) {
106 *t = '\0';
107 t++;
108 buf += snprintf(buf, p + BUFSIZE - buf, "%s='%s'\n", *ep, t);
109 --t;
110 *t = '=';
111 }
112 ep++;
113 if (buf > p + BUFSIZE)
114 break;
115 }
116
117 full:
118 buf = p;
119 write(fd, buf, strlen(buf));
120 close(fd);
121 free(buf);
122 return 0;
123
124 error:
125 fprintf(stderr, "record enviroment to /events, to be called from udev context\n");
126 return 1;
127 }