summaryrefslogtreecommitdiffstats
path: root/devices/udev-helper.c
blob: f9360860e4c8955813a741fbd5444646bbb819ce (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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <asm/byteorder.h>

#include "udev-helper.h"

#define parser_dir "."

#define tmp_dir "/var/tmp/petitboot"
#define socket_file "/var/tmp/petitboot-dev"
#define mount_bin "/bin/mount"
#define umount_bin "/bin/umount"

extern struct parser native_parser;
static FILE *logf;
static int sock;

/* array of parsers, ordered by priority */
static struct parser *parsers[] = {
	&native_parser,
	NULL
};

#define log(...) fprintf(logf, __VA_ARGS__)

static void iterate_parsers(const char *devpath, const char *mountpoint)
{
	int i;

	log("trying parsers for %s@%s\n", devpath, mountpoint);

	for (i = 0; parsers[i]; i++) {
		log("\ttrying parser '%s'\n", parsers[i]->name);
		/* just use a dummy device path for now */
		if (parsers[i]->parse(devpath, mountpoint))
			return;
	}
	log("\tno boot_options found\n");
}

static void print_boot_option(const struct boot_option *opt)
{
	log("\tname: %s\n", opt->name);
	log("\tdescription: %s\n", opt->description);
	log("\tboot_image: %s\n", opt->boot_image_file);
	log("\tboot_args: %s\n", opt->boot_args);

}

static void print_device(const struct device *dev)
{
	log("\tid: %s\n", dev->name);
	log("\tname: %s\n", dev->name);
	log("\tdescription: %s\n", dev->description);
	log("\tboot_image: %s\n", dev->icon_file);
}


void free_device(struct device *dev)
{
	if (!dev)
		return;
	if (dev->id)
		free(dev->id);
	if (dev->name)
		free(dev->name);
	if (dev->description)
		free(dev->description);
	if (dev->icon_file)
		free(dev->icon_file);
	free(dev);
}

void free_boot_option(struct boot_option *opt)
{
	if (!opt)
		return;
	if (opt->name)
		free(opt->name);
	if (opt->description)
		free(opt->description);
	if (opt->icon_file)
		free(opt->icon_file);
	if (opt->boot_image_file)
		free(opt->boot_image_file);
	if (opt->initrd_file)
		free(opt->initrd_file);
	if (opt->boot_args)
		free(opt->boot_args);
	free(opt);
}

static int write_action(int fd, enum device_action action)
{
	uint8_t action_buf = action;
	return write(fd, &action_buf, sizeof(action_buf)) != sizeof(action_buf);
}

static int write_string(int fd, const char *str)
{
	int len, pos = 0;
	uint32_t len_buf;

	if (!str) {
		len_buf = 0;
		if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
			log("write failed: %s\n", strerror(errno));
			return -1;
		}
		return 0;
	}

	len = strlen(str);
	if (len > (1ull << (sizeof(len_buf) * 8 - 1))) {
		log("string too large\n");
		return -1;
	}

	len_buf = __cpu_to_be32(len);
	if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
		log("write failed: %s\n", strerror(errno));
		return -1;
	}

	while (pos < len) {
		int rc = write(fd, str, len - pos);
		if (rc <= 0) {
			log("write failed: %s\n", strerror(errno));
			return -1;
		}
		pos += rc;
		str += rc;
	}

	return 0;
}

int add_device(const struct device *dev)
{
	int rc;

	log("device added:\n");
	print_device(dev);
	rc = write_action(sock, DEV_ACTION_ADD_DEVICE) ||
		write_string(sock, dev->id) ||
		write_string(sock, dev->name) ||
		write_string(sock, dev->description) ||
		write_string(sock, dev->icon_file);

	if (rc)
		log("error writing device %s to socket\n", dev->name);

	return rc;
}

int add_boot_option(const struct boot_option *opt)
{
	int rc;

	log("boot option added:\n");
	print_boot_option(opt);

	rc = write_action(sock, DEV_ACTION_ADD_OPTION) ||
		write_string(sock, opt->id) ||
		write_string(sock, opt->name) ||
		write_string(sock, opt->description) ||
		write_string(sock, opt->icon_file) ||
		write_string(sock, opt->boot_image_file) ||
		write_string(sock, opt->initrd_file) ||
		write_string(sock, opt->boot_args);

	if (rc)
		log("error writing boot option %s to socket\n", opt->name);

	return rc;
}

int remove_device(const char *dev_path)
{
	return write_action(sock, DEV_ACTION_REMOVE_DEVICE) ||
		write_string(sock, dev_path);
}

int connect_to_socket()
{
#if 1
	int fd;
	struct sockaddr_un addr;

	fd = socket(PF_UNIX, SOCK_STREAM, 0);
	if (fd == -1) {
		log("can't create socket: %s\n", strerror(errno));
		return -1;
	}

	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, socket_file);

	if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
		log("can't connect to %s: %s\n",
				addr.sun_path, strerror(errno));
		return -1;
	}
	sock = fd;

	return 0;
#else
	int fd;
	fd = open("./debug_socket", O_WRONLY | O_CREAT, 0640);
	if (fd < 0) {
		log("can't create output file: %s\n", strerror(errno));
		return -1;
	}
	sock = fd;
	return 0;
#endif
}

#define template "mnt-XXXXXX"

static int mount_device(const char *dev_path, char *mount_path)
{
	char *dir;
	int pid, status, rc = -1;

	/* create a unique mountpoint */
	dir = malloc(strlen(tmp_dir) + 2 + strlen(template));
	sprintf(dir, "%s/%s", tmp_dir, template);

	if (!mkdtemp(dir)) {
		log("failed to create temporary directory in %s: %s",
				tmp_dir, strerror(errno));
		goto out;
	}

	pid = fork();
	if (pid == -1) {
		log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
		goto out;
	}

	if (pid == 0) {
		execl(mount_bin, mount_bin, dev_path, dir, "-o", "ro", NULL);
		exit(EXIT_FAILURE);
	}

	if (waitpid(pid, &status, 0) == -1) {
		log("%s: waitpid failed: %s\n", __FUNCTION__, strerror(errno));
		goto out;
	}

	if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
		strcpy(mount_path, dir);
		rc = 0;
	}

out:
	free(dir);
	return rc;
}

static int unmount_device(const char *dev_path)
{
	int pid, status, rc;

	pid = fork();

	if (pid == -1) {
		log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
		return -1;
	}

	if (pid == 0) {
		execl(umount_bin, umount_bin, dev_path, NULL);
		exit(EXIT_FAILURE);
	}

	if (waitpid(pid, &status, 0) == -1) {
		log("%s: waitpid failed: %s\n", __FUNCTION__, strerror(errno));
		return -1;
	}

	rc = !WIFEXITED(status) || WEXITSTATUS(status) != 0;

	return rc;
}

const char *generic_icon_file(enum generic_icon_type type)
{
	switch (type) {
	case ICON_TYPE_DISK:
		return "artwork/hdd.png";
	case ICON_TYPE_USB:
		return "artwork/usbpen.png";
	case ICON_TYPE_OPTICAL:
		return "artwork/cdrom.png";
	case ICON_TYPE_NETWORK:
	case ICON_TYPE_UNKNOWN:
		break;
	}
	return "artwork/hdd.png";
}

static const struct device fake_boot_devices[] =
{
	{
		.id		= "fakeDisk0",
		.name		= "Hard Disk",
		.icon_file	= "artwork/hdd.png",
	},
	{
		.id		= "fakeDisk1",
		.name		= "PinkCat Linux CD",
		.icon_file	= "artwork/cdrom.png",
	}
};

static const struct boot_option fake_boot_options[] =
{
	{
		.id		= "fakeBoot0",
		.name		= "Bloobuntu Linux",
		.description	= "Boot Bloobuntu Linux",
		.icon_file	= "artwork/hdd.png",
	},
	{
		.id		= "fakeBoot1",
		.name		= "Pendora Gore 6",
		.description	= "Boot Pendora Gora 6",
		.icon_file	= "artwork/hdd.png",
	},
	{
		.id		= "fakeBoot2",
		.name		= "Genfoo Minux",
		.description	= "Boot Genfoo Minux",
		.icon_file	= "artwork/hdd.png",
	},
	{
		.id		= "fakeBoot3",
		.name		= "PinkCat Linux",
		.description	= "Install PinkCat Linux - Graphical install",
		.icon_file	= "artwork/cdrom.png",
	},
};

enum generic_icon_type guess_device_type(void)
{
	const char *bus = getenv("ID_BUS");
	if (streq(bus, "usb"))
		return ICON_TYPE_USB;
	if (streq(bus, "ata") || streq(bus, "scsi"))
		return ICON_TYPE_DISK;
	return ICON_TYPE_UNKNOWN;
}

int main(int argc, char **argv)
{
	char mountpoint[PATH_MAX];
	char *dev_path, *action;
	int rc;

	/*if (fork())
		return EXIT_SUCCESS;
		*/
	action = getenv("ACTION");

	logf = stdout;
	rc = EXIT_SUCCESS;

	if (!action) {
		log("missing environment?\n");
		return EXIT_FAILURE;
	}

	if (connect_to_socket())
		return EXIT_FAILURE;

	if (streq(action, "fake")) {
		log("fake mode");

		add_device(&fake_boot_devices[0]);
		add_boot_option(&fake_boot_options[0]);
		add_boot_option(&fake_boot_options[1]);
		add_boot_option(&fake_boot_options[2]);
		add_device(&fake_boot_devices[1]);
		add_boot_option(&fake_boot_options[3]);

		return EXIT_SUCCESS;
	}

	dev_path = getenv("DEVNAME");
	if (!dev_path) {
		log("missing environment?\n");
		return EXIT_FAILURE;
	}

	if (streq(action, "add")) {
		if (mount_device(dev_path, mountpoint)) {
			log("failed to mount %s\n", dev_path);
			return EXIT_FAILURE;
		}

		log("mounted %s at %s\n", dev_path, mountpoint);

		iterate_parsers(dev_path, mountpoint);

	} else if (streq(action, "remove")) {
		log("%s removed\n", dev_path);

		remove_device(dev_path);

		unmount_device(dev_path);

	} else {
		log("invalid action '%s'\n", action);
		rc = EXIT_FAILURE;
	}
	return rc;
}
OpenPOWER on IntegriCloud