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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
/* cdsymlinks.c
*
* Map cdrom, cd-r, cdrw, dvd, dvdrw, dvdram to suitable devices.
* Prefers cd* for DVD-incapable and cdrom and dvd for read-only devices.
* First parameter is the kernel device name.
* Second parameter, if present, must be "-d" => output the full mapping.
*
* Usage:
* BUS="ide", KERNEL="hd[a-z]", PROGRAM="/etc/udev/cdsymlinks.sh %k", SYMLINK="%c{1} %c{2} %c{3} %c{4} %c{5} %c{6}"
* BUS="scsi", KERNEL="sr[0-9]*", PROGRAM="/etc/udev/cdsymlinks.sh %k", SYMLINK="%c{1} %c{2} %c{3} %c{4} %c{5} %c{6}"
* BUS="scsi", KERNEL="scd[0-9]*", PROGRAM="/etc/udev/cdsymlinks.sh %k", SYMLINK="%c{1} %c{2} %c{3} %c{4} %c{5} %c{6}"
* (this last one is "just in case")
*
* (c) 2004, 2005 Darren Salt <linux@youmustbejoking.demon.co.uk>
*
* Contributors:
* - J A Magallon <jamagallon@able.es> (bug fixes)
*
* Last modified: 2005-02-15
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <strings.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <wordexp.h>
static const char *progname;
/* This file provides us with our devices and capabilities information. */
#define CDROM_INFO "/proc/sys/dev/cdrom/info"
/* This file contains our default settings. */
#define CONFIGURATION "/etc/udev/cdsymlinks.conf"
/* Default output types configuration, in the presence of an empty list */
#define OUTPUT_DEFAULT "CD CDRW DVD DVDRW DVDRAM"
static int debug = 0;
/* List item */
struct list_item_t {
struct list_item_t *next;
char *data;
};
/* List root. Note offset of list_t->head and list_item_t->next */
struct list_t {
struct list_item_t *head, *tail;
};
/* Configuration variables */
static struct list_t allowed_output = {0};
static int numbered_links = 1;
static int link_zero = 0;
/* Available devices */
static struct list_t Devices = {0};
/* Devices' capabilities in full (same order as available devices list).
* There's no cap_CD; all are assumed able to read CDs.
*/
static struct list_t cap_DVDRAM = {0}, cap_DVDRW = {0}, cap_DVD = {0},
cap_CDRW = {0}, cap_CDR = {0}, cap_CDWMRW = {0},
cap_CDMRW = {0}, cap_CDRAM = {0};
/* Device capabilities by name */
static struct list_t dev_DVDRAM = {0}, dev_DVDRW = {0}, dev_DVD = {0},
dev_CDRW = {0}, dev_CDR = {0}, dev_CDWMRW = {0},
dev_CDMRW = {0}, dev_CDRAM = {0};
#define dev_CD Devices
typedef struct {
struct list_t *cap, *dev;
const char label[8], symlink[8];
const char *captext;
int captextlen;
} cap_dev_t;
#define CAPDEV(X) &cap_##X, &dev_##X
static const cap_dev_t cap_dev_info[] = {
{ NULL, &dev_CD, "CD", "cdrom", NULL, 0 },
{ CAPDEV(CDR), "CDR", "cd-r", "Can write CD-R:", 15 },
{ CAPDEV(CDRW), "CDRW", "cdrw", "Can write CD-RW:", 16 },
{ CAPDEV(DVD), "DVD", "dvd", "Can read DVD:", 13 },
{ CAPDEV(DVDRW), "DVDRW", "dvdrw", "Can write DVD-R:", 16 },
{ CAPDEV(DVDRAM), "DVDRAM", "dvdram", "Can write DVD-RAM:", 18 },
{ CAPDEV(CDMRW), "CDMRW", "cdm", "Can read MRW:", 13 }, /* CDC-MRW R */
{ CAPDEV(CDWMRW), "CDWMRW", "cdmrw", "Can write MRW:", 14 }, /* CDC-MRW W */
{ CAPDEV(CDRAM), "CDRAM", "cdram", "Can write RAM:", 14 }, /* CDC-RAM W */
{ NULL }
};
#define foreach_cap_dev(loop) \
for ((loop) = cap_dev_info; (loop)->label[0]; ++(loop))
#define foreach_cap_dev_noCD(loop) \
for ((loop) = cap_dev_info + 1; (loop)->label[0]; ++(loop))
/*
* Some library-like bits first...
*/
static void
errexit (const char *reason)
{
fprintf (stderr, "%s: %s: %s\n", progname, reason, strerror (errno));
exit (2);
}
static void
msgexit (const char *reason)
{
fprintf (stderr, "%s: %s\n", progname, reason);
exit (2);
}
static void
errwarn (const char *reason)
{
fprintf (stderr, "%s: warning: %s: %s\n", progname, reason, strerror (errno));
}
static void
msgwarn (const char *reason)
{
fprintf (stderr, "%s: warning: %s\n", progname, reason);
}
static void *
xmalloc (size_t size)
{
void *mem = malloc (size);
if (size && !mem)
msgexit ("malloc failed");
return mem;
}
static char *
xstrdup (const char *text)
{
char *mem = xmalloc (strlen (text) + 1);
return strcpy (mem, text);
}
/* Append a string to a list. The string is duplicated. */
static void
list_append (struct list_t *list, const char *data)
{
struct list_item_t *node = xmalloc (sizeof (struct list_item_t));
node->next = NULL;
if (list->tail)
list->tail->next = node;
list->tail = node;
if (!list->head)
list->head = node;
node->data = xstrdup (data);
}
/* Prepend a string to a list. The string is duplicated. */
static void
list_prepend (struct list_t *list, const char *data)
{
struct list_item_t *node = xmalloc (sizeof (struct list_item_t));
node->next = list->head;
list->head = node;
if (!list->tail)
list->tail = node;
node->data = xstrdup (data);
}
/* Delete a lists's contents, freeing claimed memory */
static void
list_delete (struct list_t *list)
{
struct list_item_t *node = list->head;
while (node)
{
struct list_item_t *n = node;
node = node->next;
free (n->data);
free (n);
}
list->tail = list->head = NULL;
}
/* Print out a list on one line, each item space-prefixed, no LF */
static void
list_print (const struct list_t *list, FILE *stream)
{
const struct list_item_t *node = (const struct list_item_t *)list;
while ((node = node->next) != NULL)
fprintf (stream, " %s", node->data);
}
/* Return the nth item in a list (count from 0)
* If there aren't enough items in the list, return the requested default
*/
static const struct list_item_t *
list_nth (const struct list_t *list, size_t nth)
{
const struct list_item_t *node = list->head;
while (nth && node)
{
node = node->next;
--nth;
}
return node;
}
/* Return the first matching item in a list, or NULL */
static const struct list_item_t *
list_search (const struct list_t *list, const char *data)
{
const struct list_item_t *node = list->head;
while (node)
{
if (!strcmp (node->data, data))
return node;
node = node->next;
}
return NULL;
}
/* Split up a string on whitespace & assign the resulting tokens to a list.
* Ignore everything up until the first colon (if present).
*/
static void
list_assign_split (struct list_t *list, char *text)
{
char *token = strchr (text, ':');
token = strtok (token ? token + 1 : text, " \t\n");
while (token)
{
list_prepend (list, token);
token = strtok (0, " \t\n");
}
}
/* Gather the default settings. */
static void
read_defaults (void)
{
FILE *conf = fopen (CONFIGURATION, "r");
if (!conf)
{
if (errno != ENOENT)
errwarn ("error accessing configuration");
}
else
{
char *text = NULL;
size_t textlen;
while (getline (&text, &textlen, conf) != -1)
{
wordexp_t p = {0};
int len = strlen (text);
if (len && text[len - 1] == '\n')
text[--len] = '\0';
if (len && text[len - 1] == '\r')
text[--len] = '\0';
if (!len)
continue;
char *token = text + strspn (text, " \t");
if (!*token || *token == '#')
continue;
switch (len = wordexp (text, &p, 0))
{
case WRDE_NOSPACE:
msgexit ("malloc failed");
case 0:
if (p.we_wordc == 1)
{
if (!strncmp (p.we_wordv[0], "OUTPUT=", 7))
{
list_delete (&allowed_output);
list_assign_split (&allowed_output, p.we_wordv[0] + 7);
}
else if (!strncmp (p.we_wordv[0], "NUMBERED_LINKS=", 15))
numbered_links = atoi (p.we_wordv[0] + 15);
else if (!strncmp (p.we_wordv[0], "LINK_ZERO=", 15))
link_zero = atoi (p.we_wordv[0] + 15);
break;
}
/* fall through */
default:
msgwarn ("syntax error in configuration file");
}
wordfree (&p);
}
if (!feof (conf))
errwarn ("error accessing configuration");
if (fclose (conf))
errwarn ("error accessing configuration");
free (text);
}
if (!allowed_output.head)
{
char *dflt = strdup (OUTPUT_DEFAULT);
list_assign_split (&allowed_output, dflt);
free (dflt);
}
}
/* From information supplied by the kernel:
* + get the names of the available devices
* + populate our capability lists
* Order is significant: device item N maps to each capability item N.
*/
static void
populate_capability_lists (void)
{
FILE *info = fopen (CDROM_INFO, "r");
if (!info)
{
if (errno == ENOENT)
exit (0);
errexit ("error accessing CD/DVD info");
}
char *text = 0;
size_t textlen = 0;
while (getline (&text, &textlen, info) != -1)
{
if (!strncasecmp (text, "drive name", 10))
list_assign_split (&Devices, text);
else
{
const cap_dev_t *cap;
foreach_cap_dev_noCD (cap)
if (!strncasecmp (text, cap->captext, cap->captextlen))
{
list_assign_split (cap->cap, text);
break;
}
}
}
if (!feof (info))
errexit ("error accessing CD/DVD info");
fclose (info);
free (text);
}
/* Write out the links of type LINK which should be created for device NAME,
* taking into account existing links and the capability list for type LINK.
*/
static void
do_output (const char *name, const char *link, const struct list_t *dev,
int do_link_zero)
{
const struct list_item_t *i = (const struct list_item_t *)dev;
if (!i->next)
return;
errno = 0;
size_t link_len = strlen (link);
DIR *dir = opendir ("/dev");
if (!dir)
errexit ("error reading /dev");
struct list_t devls = {0}; /* symlinks whose name matches LINK */
struct list_t devlinks = {0}; /* those symlinks' targets */
struct dirent *entry;
while ((entry = readdir (dir)) != NULL)
{
if (strncmp (entry->d_name, link, link_len))
continue; /* wrong name: ignore it */
/* The rest of the name must be null or consist entirely of digits. */
const char *p = entry->d_name + link_len - 1;
while (*++p)
if (!isdigit (*p))
break;
if (*p)
continue; /* wrong format - ignore */
/* Assume that it's a symlink and try to read its target. */
char buf[sizeof (entry->d_name)];
int r = readlink (entry->d_name, buf, sizeof (buf) - 1);
if (r < 0)
{
if (errno == EINVAL)
continue; /* not a symlink - ignore */
errexit ("error reading link in /dev");
}
/* We have the name and the target, so update our lists. */
buf[r] = 0;
list_append (&devls, entry->d_name);
list_append (&devlinks, buf);
}
if (errno)
errexit ("error reading /dev");
if (closedir (dir))
errexit ("error closing /dev");
/* Now we write our output... */
size_t count = 0;
while ((i = i->next) != NULL)
{
int isdev = !strcmp (name, i->data); /* current dev == target dev? */
int present = 0;
size_t li = -1;
const struct list_item_t *l = (const struct list_item_t *)&devlinks;
/* First, we look for existing symlinks to the target device. */
while (++li, (l = l->next) != NULL)
{
if (strcmp (l->data, i->data))
continue;
/* Existing symlink found - don't output a new one.
* If ISDEV, we output the name of the existing symlink.
*/
if (do_link_zero)
return;
present = 1;
if (isdev)
printf (" %s", list_nth (&devls, li)->data);
}
/* If we found no existing symlinks for the target device... */
if (!present)
{
char buf[256];
snprintf (buf, sizeof (buf), count || do_link_zero ? "%s%d" : "%s",
link, count);
/* Find the next available (not present) symlink name.
* We always need to do this for reasons of output consistency: if a
* symlink is created by udev as a result of use of this program, we
* DON'T want different output!
*/
while (list_search (&devls, buf))
{
if (do_link_zero)
return;
snprintf (buf, sizeof (buf), "%s%d", link, ++count);
}
/* If ISDEV, output it. */
if (isdev && (numbered_links || count == 0))
printf (" %s", buf);
/* If the link isn't in our "existing links" list, add it and increment
* our counter.
*/
if (!list_search (&devls, buf))
{
if (do_link_zero)
return;
list_append (&devls, buf);
++count;
}
}
}
list_delete (&devls);
list_delete (&devlinks);
}
/* Populate a device list from a capabilities list. */
static void
populate_device_list (struct list_t *out, const struct list_t *caps)
{
const struct list_item_t *cap, *dev;
cap = (const struct list_item_t *)caps;
dev = (const struct list_item_t *)&Devices;
while ((cap = cap->next) != NULL && (dev = dev->next) != NULL)
if (cap->data[0] != '0')
list_append (out, dev->data);
}
int
main (int argc, char *argv[])
{
const cap_dev_t *capdev;
progname = argv[0];
debug = argc > 2 && !strcmp (argv[2], "-d");
if (argc < 2 || argc > 2 + debug)
msgexit ("usage: cdsymlinks DEVICE [-d]");
if (chdir ("/dev"))
errexit ("can't chdir /dev");
read_defaults ();
populate_capability_lists ();
/* Construct the device lists from the capability lists.
* (We assume that all relevant devices can read CDs.)
*/
foreach_cap_dev_noCD (capdev)
populate_device_list (capdev->dev, capdev->cap);
if (debug)
{
printf ("Devices:");
const struct list_item_t *item = (const struct list_item_t *)&Devices;
while ((item = item->next) != NULL)
printf (" %s", item->data);
printf ("\nCDROM : (all)");
item = (const struct list_item_t *)&dev_CD;
while ((item = item->next) != NULL)
printf (" %s", item->data);
puts ("");
foreach_cap_dev_noCD (capdev)
{
printf ("%-10s:", capdev->label);
list_print (capdev->cap, stdout);
list_print (capdev->dev, stdout);
puts ("");
}
}
/* Write the symlink names. */
foreach_cap_dev (capdev)
if (list_search (&allowed_output, capdev->label))
{
do_output (argv[1], capdev->symlink, capdev->dev, 0);
if (link_zero)
do_output (argv[1], capdev->symlink, capdev->dev, 1);
}
puts ("");
return 0;
}
|