summaryrefslogtreecommitdiff
path: root/udeveventrecorder.c
blob: 8ac344a47e82979d66550c2320416660b3eb3e3c (plain)
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
/*
 * udeveventrecorder.c
 *
 * Copyright (C) 2004-2005 SuSE Linux Products GmbH
 * Author:
 *	Olaf Hering <olh@suse.de>
 *
 *	This program is free software; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the
 *	Free Software Foundation version 2 of the License.
 * 
 *	This program 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
 *	General Public License for more details.
 * 
 *	You should have received a copy of the GNU General Public License along
 *	with this program; if not, write to the Free Software Foundation, Inc.,
 *	675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "udev.h"
#include "udev_version.h"
#include "udev_utils.h"
#include "logging.h"

#define BUFSIZE 12345
#define FNSIZE  123

static int udev_log = 0;

#ifdef USE_LOG
void log_message (int priority, const char *format, ...)
{
	va_list	args;

	if (priority > udev_log)
		return;

	va_start(args, format);
	vsyslog(priority, format, args);
	va_end(args);
}
#endif

int main(int argc, char **argv, char **envp)
{
	int fd, i;
	unsigned long seq;
	char **ep = envp;
	char *buf, *p, *a;
	struct stat sb;
	const char *env;

	if (stat("/events", &sb) || !(S_ISDIR(sb.st_mode)))
		return 0;

	env = getenv("UDEV_LOG");
	if (env)
		udev_log = log_priority(env);

	logging_init("udeveventrecorder");
	dbg("version %s", UDEV_VERSION);

	p = getenv("SEQNUM");
	a = getenv("ACTION");
	buf = malloc(FNSIZE);
	if (!(buf && a && argv[1]))
		goto error;
	if (p)
		seq = strtoul(p, NULL, 0);
	else
		seq = 0;

	snprintf(buf, FNSIZE, "/events/debug.%05lu.%s.%s.%u", seq, argv[1], a ? a : "", getpid());
	if ((fd = open(buf, O_CREAT | O_WRONLY | O_TRUNC, 0644)) < 0) {
		err("error creating '%s'", buf);
		goto error;
	}
	free(buf);
	p = malloc(BUFSIZE);
	buf = p;
	buf += snprintf(buf, p + BUFSIZE - buf, "set --");
	for (i = 1; i < argc; ++i) {
		buf += snprintf(buf, p + BUFSIZE - buf, " %s", argv[i]);
		if (buf > p + BUFSIZE)
			goto full;
	}
	buf += snprintf(buf, p + BUFSIZE - buf, "\n");
	if (buf > p + BUFSIZE)
		goto full;
	while (*ep) {
		unsigned char *t;
		t = memchr(*ep, '=', strlen(*ep));
		if (t) {
			*t = '\0';
			t++;
			buf += snprintf(buf, p + BUFSIZE - buf, "%s='%s'\n", *ep, t);
			--t;
			*t = '=';
		}
		ep++;
		if (buf > p + BUFSIZE)
			break;
	}

full:
	buf = p;
	write(fd, buf, strlen(buf));
	close(fd);
	free(buf);
	return 0;

error:
	fprintf(stderr, "record enviroment to /events, to be called from udev context\n");
	return 1;
}