-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevfsd.c
1809 lines (1607 loc) · 54.2 KB
/
devfsd.c
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi: set sw=4 ts=4: */
/*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
/*
devfsd implementation for busybox
Copyright (C) 2003 by Tito Ragusa <[email protected]>
Busybox version is based on some previous work and ideas
Copyright (C) [2003] by [Matteo Croce] <[email protected]>
devfsd.c
Main file for devfsd (devfs daemon for Linux).
Copyright (C) 1998-2002 Richard Gooch
devfsd.h
Header file for devfsd (devfs daemon for Linux).
Copyright (C) 1998-2000 Richard Gooch
compat_name.c
Compatibility name file for devfsd (build compatibility names).
Copyright (C) 1998-2002 Richard Gooch
expression.c
This code provides Borne Shell-like expression expansion.
Copyright (C) 1997-1999 Richard Gooch
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; either version 2 of the License, or
(at your option) any later version.
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.
Richard Gooch may be reached by email at [email protected]
The postal address is:
Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
*/
//usage:#define devfsd_trivial_usage
//usage: "mntpnt [-v]" IF_DEVFSD_FG_NP("[-fg][-np]")
//usage:#define devfsd_full_usage "\n\n"
//usage: "Manage devfs permissions and old device name symlinks\n"
//usage: "\n mntpnt The mount point where devfs is mounted"
//usage: "\n -v Print the protocol version numbers for devfsd"
//usage: "\n and the kernel-side protocol version and exit"
//usage: IF_DEVFSD_FG_NP(
//usage: "\n -fg Run in foreground"
//usage: "\n -np Exit after parsing the configuration file"
//usage: "\n and processing synthetic REGISTER events,"
//usage: "\n don't poll for events"
//usage: )
#include "libbb.h"
#include "xregex.h"
#include <syslog.h>
#include <sys/un.h>
#include <sys/sysmacros.h>
/* Various defines taken from linux/major.h */
#define IDE0_MAJOR 3
#define IDE1_MAJOR 22
#define IDE2_MAJOR 33
#define IDE3_MAJOR 34
#define IDE4_MAJOR 56
#define IDE5_MAJOR 57
#define IDE6_MAJOR 88
#define IDE7_MAJOR 89
#define IDE8_MAJOR 90
#define IDE9_MAJOR 91
/* Various defines taken from linux/devfs_fs.h */
#define DEVFSD_PROTOCOL_REVISION_KERNEL 5
#define DEVFSD_IOCTL_BASE 'd'
/* These are the various ioctls */
#define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int)
#define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int)
#define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int)
#define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
#define DEVFSD_NOTIFY_REGISTERED 0
#define DEVFSD_NOTIFY_UNREGISTERED 1
#define DEVFSD_NOTIFY_ASYNC_OPEN 2
#define DEVFSD_NOTIFY_CLOSE 3
#define DEVFSD_NOTIFY_LOOKUP 4
#define DEVFSD_NOTIFY_CHANGE 5
#define DEVFSD_NOTIFY_CREATE 6
#define DEVFSD_NOTIFY_DELETE 7
#define DEVFS_PATHLEN 1024
/* Never change this otherwise the binary interface will change */
struct devfsd_notify_struct {
/* Use native C types to ensure same types in kernel and user space */
unsigned int type; /* DEVFSD_NOTIFY_* value */
unsigned int mode; /* Mode of the inode or device entry */
unsigned int major; /* Major number of device entry */
unsigned int minor; /* Minor number of device entry */
unsigned int uid; /* Uid of process, inode or device entry */
unsigned int gid; /* Gid of process, inode or device entry */
unsigned int overrun_count; /* Number of lost events */
unsigned int namelen; /* Number of characters not including '\0' */
/* The device name MUST come last */
char devname[DEVFS_PATHLEN]; /* This will be '\0' terminated */
};
#define BUFFER_SIZE 16384
#define DEVFSD_VERSION "1.3.25"
#define CONFIG_FILE "/etc/devfsd.conf"
#define MODPROBE "/sbin/modprobe"
#define MODPROBE_SWITCH_1 "-k"
#define MODPROBE_SWITCH_2 "-C"
#define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
#define MAX_ARGS (6 + 1)
#define MAX_SUBEXPR 10
#define STRING_LENGTH 255
/* for get_uid_gid() */
#define UID 0
#define GID 1
/* fork_and_execute() */
# define DIE 1
# define NO_DIE 0
/* for dir_operation() */
#define RESTORE 0
#define SERVICE 1
#define READ_CONFIG 2
/* Update only after changing code to reflect new protocol */
#define DEVFSD_PROTOCOL_REVISION_DAEMON 5
/* Compile-time check */
#if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
#error protocol version mismatch. Update your kernel headers
#endif
#define AC_PERMISSIONS 0
#define AC_MODLOAD 1
#define AC_EXECUTE 2
#define AC_MFUNCTION 3 /* not supported by busybox */
#define AC_CFUNCTION 4 /* not supported by busybox */
#define AC_COPY 5
#define AC_IGNORE 6
#define AC_MKOLDCOMPAT 7
#define AC_MKNEWCOMPAT 8
#define AC_RMOLDCOMPAT 9
#define AC_RMNEWCOMPAT 10
#define AC_RESTORE 11
struct permissions_type {
mode_t mode;
uid_t uid;
gid_t gid;
};
struct execute_type {
char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */
};
struct copy_type {
const char *source;
const char *destination;
};
struct action_type {
unsigned int what;
unsigned int when;
};
struct config_entry_struct {
struct action_type action;
regex_t preg;
union
{
struct permissions_type permissions;
struct execute_type execute;
struct copy_type copy;
}
u;
struct config_entry_struct *next;
};
struct get_variable_info {
const struct devfsd_notify_struct *info;
const char *devname;
char devpath[STRING_LENGTH];
};
static void dir_operation(int , const char * , int, unsigned long*);
static void service(struct stat statbuf, char *path);
static int st_expr_expand(char *, unsigned, const char *, const char *(*)(const char *, void *), void *);
static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
static int mksymlink(const char *oldpath, const char *newpath);
static void read_config_file(char *path, int optional, unsigned long *event_mask);
static void process_config_line(const char *, unsigned long *);
static int do_servicing(int, unsigned long);
static void service_name(const struct devfsd_notify_struct *);
static void action_permissions(const struct devfsd_notify_struct *, const struct config_entry_struct *);
static void action_execute(const struct devfsd_notify_struct *, const struct config_entry_struct *,
const regmatch_t *, unsigned);
static void action_modload(const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
static void action_copy(const struct devfsd_notify_struct *, const struct config_entry_struct *,
const regmatch_t *, unsigned);
static void action_compat(const struct devfsd_notify_struct *, unsigned);
static void free_config(void);
static void restore(char *spath, struct stat source_stat, int rootlen);
static int copy_inode(const char *, const struct stat *, mode_t, const char *, const struct stat *);
static mode_t get_mode(const char *);
static void signal_handler(int);
static const char *get_variable(const char *, void *);
static int make_dir_tree(const char *);
static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
const char *, const regmatch_t *, unsigned);
static void expand_regexp(char *, size_t, const char *, const char *, const regmatch_t *, unsigned);
static const char *expand_variable( char *, unsigned, unsigned *, const char *,
const char *(*)(const char *, void *), void *);
static const char *get_variable_v2(const char *, const char *(*)(const char *, void *), void *);
static char get_old_ide_name(unsigned, unsigned);
static char *write_old_sd_name(char *, unsigned, unsigned, const char *);
/* busybox functions */
static int get_uid_gid(int flag, const char *string);
static void safe_memcpy(char * dest, const char * src, int len);
static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr);
static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr);
/* Structs and vars */
static struct config_entry_struct *first_config = NULL;
static struct config_entry_struct *last_config = NULL;
static char *mount_point = NULL;
static volatile int caught_signal = FALSE;
static volatile int caught_sighup = FALSE;
static struct initial_symlink_struct {
const char *dest;
const char *name;
} initial_symlinks[] = {
{"/proc/self/fd", "fd"},
{"fd/0", "stdin"},
{"fd/1", "stdout"},
{"fd/2", "stderr"},
{NULL, NULL},
};
static struct event_type {
unsigned int type; /* The DEVFSD_NOTIFY_* value */
const char *config_name; /* The name used in the config file */
} event_types[] = {
{DEVFSD_NOTIFY_REGISTERED, "REGISTER"},
{DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
{DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"},
{DEVFSD_NOTIFY_CLOSE, "CLOSE"},
{DEVFSD_NOTIFY_LOOKUP, "LOOKUP"},
{DEVFSD_NOTIFY_CHANGE, "CHANGE"},
{DEVFSD_NOTIFY_CREATE, "CREATE"},
{DEVFSD_NOTIFY_DELETE, "DELETE"},
{0xffffffff, NULL}
};
/* Busybox messages */
static const char bb_msg_proto_rev[] ALIGN1 = "protocol revision";
static const char bb_msg_bad_config[] ALIGN1 = "bad %s config file: %s";
static const char bb_msg_small_buffer[] ALIGN1 = "buffer too small";
static const char bb_msg_variable_not_found[] ALIGN1 = "variable: %s not found";
/* Busybox stuff */
#if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG
#define info_logger(p, fmt, args...) bb_info_msg(fmt, ## args)
#define msg_logger(p, fmt, args...) bb_error_msg(fmt, ## args)
#define msg_logger_and_die(p, fmt, args...) bb_error_msg_and_die(fmt, ## args)
#define error_logger(p, fmt, args...) bb_perror_msg(fmt, ## args)
#define error_logger_and_die(p, fmt, args...) bb_perror_msg_and_die(fmt, ## args)
#else
#define info_logger(p, fmt, args...)
#define msg_logger(p, fmt, args...)
#define msg_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE)
#define error_logger(p, fmt, args...)
#define error_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE)
#endif
static void safe_memcpy(char *dest, const char *src, int len)
{
memcpy(dest , src, len);
dest[len] = '\0';
}
static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr)
{
if (d[n - 4] == 'd' && d[n - 3] == 'i' && d[n - 2] == 's' && d[n - 1] == 'c')
return 2 + addendum;
if (d[n - 2] == 'c' && d[n - 1] == 'd')
return 3 + addendum;
if (ptr[0] == 'p' && ptr[1] == 'a' && ptr[2] == 'r' && ptr[3] == 't')
return 4 + addendum;
if (ptr[n - 2] == 'm' && ptr[n - 1] == 't')
return 5 + addendum;
return 0;
}
static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr)
{
if (d[0] == 's' && d[1] == 'c' && d[2] == 's' && d[3] == 'i' && d[4] == '/') {
if (d[n - 7] == 'g' && d[n - 6] == 'e' && d[n - 5] == 'n'
&& d[n - 4] == 'e' && d[n - 3] == 'r' && d[n - 2] == 'i' && d[n - 1] == 'c'
)
return 1;
return scan_dev_name_common(d, n, 0, ptr);
}
if (d[0] == 'i' && d[1] == 'd' && d[2] == 'e' && d[3] == '/'
&& d[4] == 'h' && d[5] == 'o' && d[6] == 's' && d[7] == 't'
)
return scan_dev_name_common(d, n, 4, ptr);
if (d[0] == 's' && d[1] == 'b' && d[2] == 'p' && d[3] == '/')
return 10;
if (d[0] == 'v' && d[1] == 'c' && d[2] == 'c' && d[3] == '/')
return 11;
if (d[0] == 'p' && d[1] == 't' && d[2] == 'y' && d[3] == '/')
return 12;
return 0;
}
/* Public functions follow */
int devfsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int devfsd_main(int argc, char **argv)
{
int print_version = FALSE;
int do_daemon = TRUE;
int no_polling = FALSE;
int do_scan;
int fd, proto_rev, count;
unsigned long event_mask = 0;
struct sigaction new_action;
struct initial_symlink_struct *curr;
if (argc < 2)
bb_show_usage();
for (count = 2; count < argc; ++count) {
if (argv[count][0] == '-') {
if (argv[count][1] == 'v' && !argv[count][2]) /* -v */
print_version = TRUE;
else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'f'
&& argv[count][2] == 'g' && !argv[count][3]) /* -fg */
do_daemon = FALSE;
else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'n'
&& argv[count][2] == 'p' && !argv[count][3]) /* -np */
no_polling = TRUE;
else
bb_show_usage();
}
}
mount_point = bb_simplify_path(argv[1]);
xchdir(mount_point);
fd = xopen(".devfsd", O_RDONLY);
close_on_exec_on(fd);
xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev);
/*setup initial entries */
for (curr = initial_symlinks; curr->dest != NULL; ++curr)
symlink(curr->dest, curr->name);
/* NB: The check for CONFIG_FILE is done in read_config_file() */
if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) {
printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
applet_name, DEVFSD_VERSION, bb_msg_proto_rev,
DEVFSD_PROTOCOL_REVISION_DAEMON, bb_msg_proto_rev, proto_rev);
if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev);
exit(EXIT_SUCCESS); /* -v */
}
/* Tell kernel we are special(i.e. we get to see hidden entries) */
xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
/* Set up SIGHUP and SIGUSR1 handlers */
sigemptyset(&new_action.sa_mask);
new_action.sa_flags = 0;
new_action.sa_handler = signal_handler;
sigaction_set(SIGHUP, &new_action);
sigaction_set(SIGUSR1, &new_action);
printf("%s v%s started for %s\n", applet_name, DEVFSD_VERSION, mount_point);
/* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
umask(0);
read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
/* Do the scan before forking, so that boot scripts see the finished product */
dir_operation(SERVICE, mount_point, 0, NULL);
if (ENABLE_DEVFSD_FG_NP && no_polling)
exit(EXIT_SUCCESS);
if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG)
logmode = LOGMODE_BOTH;
else if (do_daemon == TRUE)
logmode = LOGMODE_SYSLOG;
/* This is the default */
/*else
logmode = LOGMODE_STDIO; */
if (do_daemon) {
/* Release so that the child can grab it */
xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
bb_daemonize_or_rexec(0, argv);
} else if (ENABLE_DEVFSD_FG_NP) {
setpgid(0, 0); /* Become process group leader */
}
while (TRUE) {
do_scan = do_servicing(fd, event_mask);
free_config();
read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
if (do_scan)
dir_operation(SERVICE, mount_point, 0, NULL);
}
if (ENABLE_FEATURE_CLEAN_UP) free(mount_point);
} /* End Function main */
/* Private functions follow */
static void read_config_file(char *path, int optional, unsigned long *event_mask)
/* [SUMMARY] Read a configuration database.
<path> The path to read the database from. If this is a directory, all
entries in that directory will be read(except hidden entries).
<optional> If TRUE, the routine will silently ignore a missing config file.
<event_mask> The event mask is written here. This is not initialised.
[RETURNS] Nothing.
*/
{
struct stat statbuf;
FILE *fp;
char buf[STRING_LENGTH];
char *line = NULL;
char *p;
if (stat(path, &statbuf) == 0) {
/* Don't read 0 length files: ignored */
/*if (statbuf.st_size == 0)
return;*/
if (S_ISDIR(statbuf.st_mode)) {
p = bb_simplify_path(path);
dir_operation(READ_CONFIG, p, 0, event_mask);
free(p);
return;
}
fp = fopen_for_read(path);
if (fp != NULL) {
while (fgets(buf, STRING_LENGTH, fp) != NULL) {
/* Skip whitespace */
line = buf;
line = skip_whitespace(line);
if (line[0] == '\0' || line[0] == '#')
continue;
process_config_line(line, event_mask);
}
fclose(fp);
} else {
goto read_config_file_err;
}
} else {
read_config_file_err:
if (optional == 0 && errno == ENOENT)
error_logger_and_die(LOG_ERR, "read config file: %s", path);
}
} /* End Function read_config_file */
static void process_config_line(const char *line, unsigned long *event_mask)
/* [SUMMARY] Process a line from a configuration file.
<line> The configuration line.
<event_mask> The event mask is written here. This is not initialised.
[RETURNS] Nothing.
*/
{
int num_args, count;
struct config_entry_struct *new;
char p[MAX_ARGS][STRING_LENGTH];
char when[STRING_LENGTH], what[STRING_LENGTH];
char name[STRING_LENGTH];
const char *msg = "";
char *ptr;
int i;
/* !!!! Only Uppercase Keywords in devsfd.conf */
static const char options[] ALIGN1 =
"CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0"
"RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0"
"COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0"
"RMOLDCOMPAT\0""RMNEWCOMPAT\0";
for (count = 0; count < MAX_ARGS; ++count)
p[count][0] = '\0';
num_args = sscanf(line, "%s %s %s %s %s %s %s %s %s %s",
when, name, what,
p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
i = index_in_strings(options, when);
/* "CLEAR_CONFIG" */
if (i == 0) {
free_config();
*event_mask = 0;
return;
}
if (num_args < 2)
goto process_config_line_err;
/* "INCLUDE" & "OPTIONAL_INCLUDE" */
if (i == 1 || i == 2) {
st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL);
info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name);
read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask);
return;
}
/* "RESTORE" */
if (i == 3) {
dir_operation(RESTORE, name, strlen(name),NULL);
return;
}
if (num_args < 3)
goto process_config_line_err;
new = xzalloc(sizeof *new);
for (count = 0; event_types[count].config_name != NULL; ++count) {
if (strcasecmp(when, event_types[count].config_name) != 0)
continue;
new->action.when = event_types[count].type;
break;
}
if (event_types[count].config_name == NULL) {
msg = "WHEN in";
goto process_config_line_err;
}
i = index_in_strings(options, what);
switch (i) {
case 4: /* "PERMISSIONS" */
new->action.what = AC_PERMISSIONS;
/* Get user and group */
ptr = strchr(p[0], '.');
if (ptr == NULL) {
msg = "UID.GID";
goto process_config_line_err; /*"missing '.' in UID.GID"*/
}
*ptr++ = '\0';
new->u.permissions.uid = get_uid_gid(UID, p[0]);
new->u.permissions.gid = get_uid_gid(GID, ptr);
/* Get mode */
new->u.permissions.mode = get_mode(p[1]);
break;
case 5: /* MODLOAD */
/*This action will pass "/dev/$devname"(i.e. "/dev/" prefixed to
the device name) to the module loading facility. In addition,
the /etc/modules.devfs configuration file is used.*/
if (ENABLE_DEVFSD_MODLOAD)
new->action.what = AC_MODLOAD;
break;
case 6: /* EXECUTE */
new->action.what = AC_EXECUTE;
num_args -= 3;
for (count = 0; count < num_args; ++count)
new->u.execute.argv[count] = xstrdup(p[count]);
new->u.execute.argv[num_args] = NULL;
break;
case 7: /* COPY */
new->action.what = AC_COPY;
num_args -= 3;
if (num_args != 2)
goto process_config_line_err; /* missing path and function in line */
new->u.copy.source = xstrdup(p[0]);
new->u.copy.destination = xstrdup(p[1]);
break;
case 8: /* IGNORE */
/* FALLTROUGH */
case 9: /* MKOLDCOMPAT */
/* FALLTROUGH */
case 10: /* MKNEWCOMPAT */
/* FALLTROUGH */
case 11:/* RMOLDCOMPAT */
/* FALLTROUGH */
case 12: /* RMNEWCOMPAT */
/* AC_IGNORE 6
AC_MKOLDCOMPAT 7
AC_MKNEWCOMPAT 8
AC_RMOLDCOMPAT 9
AC_RMNEWCOMPAT 10*/
new->action.what = i - 2;
break;
default:
msg = "WHAT in";
goto process_config_line_err;
/*esac*/
} /* switch (i) */
xregcomp(&new->preg, name, REG_EXTENDED);
*event_mask |= 1 << new->action.when;
new->next = NULL;
if (first_config == NULL)
first_config = new;
else
last_config->next = new;
last_config = new;
return;
process_config_line_err:
msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line);
} /* End Function process_config_line */
static int do_servicing(int fd, unsigned long event_mask)
/* [SUMMARY] Service devfs changes until a signal is received.
<fd> The open control file.
<event_mask> The event mask.
[RETURNS] TRUE if SIGHUP was caught, else FALSE.
*/
{
ssize_t bytes;
struct devfsd_notify_struct info;
/* (void*) cast is only in order to match prototype */
xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask);
while (!caught_signal) {
errno = 0;
bytes = read(fd, (char *) &info, sizeof info);
if (caught_signal)
break; /* Must test for this first */
if (errno == EINTR)
continue; /* Yes, the order is important */
if (bytes < 1)
break;
service_name(&info);
}
if (caught_signal) {
int c_sighup = caught_sighup;
caught_signal = FALSE;
caught_sighup = FALSE;
return c_sighup;
}
msg_logger_and_die(LOG_ERR, "read error on control file");
} /* End Function do_servicing */
static void service_name(const struct devfsd_notify_struct *info)
/* [SUMMARY] Service a single devfs change.
<info> The devfs change.
[RETURNS] Nothing.
*/
{
unsigned int n;
regmatch_t mbuf[MAX_SUBEXPR];
struct config_entry_struct *entry;
if (ENABLE_DEBUG && info->overrun_count > 0)
msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
/* Discard lookups on "/dev/log" and "/dev/initctl" */
if (info->type == DEVFSD_NOTIFY_LOOKUP
&& ((info->devname[0] == 'l' && info->devname[1] == 'o'
&& info->devname[2] == 'g' && !info->devname[3])
|| (info->devname[0] == 'i' && info->devname[1] == 'n'
&& info->devname[2] == 'i' && info->devname[3] == 't'
&& info->devname[4] == 'c' && info->devname[5] == 't'
&& info->devname[6] == 'l' && !info->devname[7]))
)
return;
for (entry = first_config; entry != NULL; entry = entry->next) {
/* First check if action matches the type, then check if name matches */
if (info->type != entry->action.when
|| regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0)
continue;
for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
/* VOID */;
switch (entry->action.what) {
case AC_PERMISSIONS:
action_permissions(info, entry);
break;
case AC_MODLOAD:
if (ENABLE_DEVFSD_MODLOAD)
action_modload(info, entry);
break;
case AC_EXECUTE:
action_execute(info, entry, mbuf, n);
break;
case AC_COPY:
action_copy(info, entry, mbuf, n);
break;
case AC_IGNORE:
return;
/*break;*/
case AC_MKOLDCOMPAT:
case AC_MKNEWCOMPAT:
case AC_RMOLDCOMPAT:
case AC_RMNEWCOMPAT:
action_compat(info, entry->action.what);
break;
default:
msg_logger_and_die(LOG_ERR, "Unknown action");
}
}
} /* End Function service_name */
static void action_permissions(const struct devfsd_notify_struct *info,
const struct config_entry_struct *entry)
/* [SUMMARY] Update permissions for a device entry.
<info> The devfs change.
<entry> The config file entry.
[RETURNS] Nothing.
*/
{
struct stat statbuf;
if (stat(info->devname, &statbuf) != 0
|| chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0
|| chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0
)
error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname);
} /* End Function action_permissions */
static void action_modload(const struct devfsd_notify_struct *info,
const struct config_entry_struct *entry UNUSED_PARAM)
/* [SUMMARY] Load a module.
<info> The devfs change.
<entry> The config file entry.
[RETURNS] Nothing.
*/
{
char *argv[6];
argv[0] = (char*)MODPROBE;
argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */
argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */
argv[3] = (char*)CONFIG_MODULES_DEVFS;
argv[4] = concat_path_file("/dev", info->devname); /* device */
argv[5] = NULL;
spawn_and_wait(argv);
free(argv[4]);
} /* End Function action_modload */
static void action_execute(const struct devfsd_notify_struct *info,
const struct config_entry_struct *entry,
const regmatch_t *regexpr, unsigned int numexpr)
/* [SUMMARY] Execute a programme.
<info> The devfs change.
<entry> The config file entry.
<regexpr> The number of subexpression(start, end) offsets within the
device name.
<numexpr> The number of elements within <<regexpr>>.
[RETURNS] Nothing.
*/
{
unsigned int count;
struct get_variable_info gv_info;
char *argv[MAX_ARGS + 1];
char largv[MAX_ARGS + 1][STRING_LENGTH];
gv_info.info = info;
gv_info.devname = info->devname;
snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
for (count = 0; entry->u.execute.argv[count] != NULL; ++count) {
expand_expression(largv[count], STRING_LENGTH,
entry->u.execute.argv[count],
get_variable, &gv_info,
gv_info.devname, regexpr, numexpr);
argv[count] = largv[count];
}
argv[count] = NULL;
spawn_and_wait(argv);
} /* End Function action_execute */
static void action_copy(const struct devfsd_notify_struct *info,
const struct config_entry_struct *entry,
const regmatch_t *regexpr, unsigned int numexpr)
/* [SUMMARY] Copy permissions.
<info> The devfs change.
<entry> The config file entry.
<regexpr> This list of subexpression(start, end) offsets within the
device name.
<numexpr> The number of elements in <<regexpr>>.
[RETURNS] Nothing.
*/
{
mode_t new_mode;
struct get_variable_info gv_info;
struct stat source_stat, dest_stat;
char source[STRING_LENGTH], destination[STRING_LENGTH];
int ret = 0;
dest_stat.st_mode = 0;
if ((info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK(info->mode))
return;
gv_info.info = info;
gv_info.devname = info->devname;
snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
expand_expression(source, STRING_LENGTH, entry->u.copy.source,
get_variable, &gv_info, gv_info.devname,
regexpr, numexpr);
expand_expression(destination, STRING_LENGTH, entry->u.copy.destination,
get_variable, &gv_info, gv_info.devname,
regexpr, numexpr);
if (!make_dir_tree(destination) || lstat(source, &source_stat) != 0)
return;
lstat(destination, &dest_stat);
new_mode = source_stat.st_mode & ~S_ISVTX;
if (info->type == DEVFSD_NOTIFY_CREATE)
new_mode |= S_ISVTX;
else if ((info->type == DEVFSD_NOTIFY_CHANGE) &&(dest_stat.st_mode & S_ISVTX))
new_mode |= S_ISVTX;
ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat);
if (ENABLE_DEBUG && ret && (errno != EEXIST))
error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination);
} /* End Function action_copy */
static void action_compat(const struct devfsd_notify_struct *info, unsigned int action)
/* [SUMMARY] Process a compatibility request.
<info> The devfs change.
<action> The action to take.
[RETURNS] Nothing.
*/
{
int ret;
const char *compat_name = NULL;
const char *dest_name = info->devname;
const char *ptr;
char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
int mode, host, bus, target, lun;
unsigned int i;
char rewind_;
/* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
static const char *const fmt[] = {
NULL ,
"sg/c%db%dt%du%d", /* scsi/generic */
"sd/c%db%dt%du%d", /* scsi/disc */
"sr/c%db%dt%du%d", /* scsi/cd */
"sd/c%db%dt%du%dp%d", /* scsi/part */
"st/c%db%dt%du%dm%d%c", /* scsi/mt */
"ide/hd/c%db%dt%du%d", /* ide/host/disc */
"ide/cd/c%db%dt%du%d", /* ide/host/cd */
"ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
"ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
NULL
};
/* First construct compatibility name */
switch (action) {
case AC_MKOLDCOMPAT:
case AC_RMOLDCOMPAT:
compat_name = get_old_name(info->devname, info->namelen, compat_buf, info->major, info->minor);
break;
case AC_MKNEWCOMPAT:
case AC_RMNEWCOMPAT:
ptr = bb_basename(info->devname);
i = scan_dev_name(info->devname, info->namelen, ptr);
/* nothing found */
if (i == 0 || i > 9)
return;
sscanf(info->devname + ((i < 6) ? 5 : 4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
snprintf(dest_buf, sizeof(dest_buf), "../%s", info->devname + (( i > 5) ? 4 : 0));
dest_name = dest_buf;
compat_name = compat_buf;
/* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
if (i == 1 || i == 2 || i == 3 || i == 6 || i ==7)
sprintf(compat_buf, fmt[i], host, bus, target, lun);
/* 4 == scsi/part 8 == ide/host/part */
if (i == 4 || i == 8)
sprintf(compat_buf, fmt[i], host, bus, target, lun, atoi(ptr + 4));
/* 5 == scsi/mt */
if (i == 5) {
rewind_ = info->devname[info->namelen - 1];
if (rewind_ != 'n')
rewind_ = '\0';
mode=0;
if (ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
mode = ptr[2] - 107; /* 1 or 2 */
if (ptr[2] == 'a')
mode = 3;
sprintf(compat_buf, fmt[i], host, bus, target, lun, mode, rewind_);
}
/* 9 == ide/host/mt */
if (i == 9)
snprintf(compat_buf, sizeof(compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
/* esac */
} /* switch (action) */
if (compat_name == NULL)
return;
/* Now decide what to do with it */
switch (action) {
case AC_MKOLDCOMPAT:
case AC_MKNEWCOMPAT:
mksymlink(dest_name, compat_name);
break;
case AC_RMOLDCOMPAT:
case AC_RMNEWCOMPAT:
ret = unlink(compat_name);
if (ENABLE_DEBUG && ret)
error_logger(LOG_ERR, "unlink: %s", compat_name);
break;
/*esac*/
} /* switch (action) */
} /* End Function action_compat */
static void restore(char *spath, struct stat source_stat, int rootlen)
{
char *dpath;
struct stat dest_stat;
dest_stat.st_mode = 0;
dpath = concat_path_file(mount_point, spath + rootlen);
lstat(dpath, &dest_stat);
free(dpath);
if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX))
copy_inode(dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX), spath, &source_stat);
if (S_ISDIR(source_stat.st_mode))
dir_operation(RESTORE, spath, rootlen, NULL);
}
static int copy_inode(const char *destpath, const struct stat *dest_stat,
mode_t new_mode,
const char *sourcepath, const struct stat *source_stat)
/* [SUMMARY] Copy an inode.
<destpath> The destination path. An existing inode may be deleted.
<dest_stat> The destination stat(2) information.
<new_mode> The desired new mode for the destination.
<sourcepath> The source path.
<source_stat> The source stat(2) information.
[RETURNS] TRUE on success, else FALSE.
*/
{
int source_len, dest_len;
char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
int fd, val;
struct sockaddr_un un_addr;
char symlink_val[STRING_LENGTH];
if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) {
/* Same type */
if (S_ISLNK(source_stat->st_mode)) {
source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1);
if ((source_len < 0)
|| (dest_len = readlink(destpath, dest_link, STRING_LENGTH - 1)) < 0
)
return FALSE;
source_link[source_len] = '\0';
dest_link[dest_len] = '\0';
if ((source_len != dest_len) || (strcmp(source_link, dest_link) != 0)) {
unlink(destpath);
symlink(source_link, destpath);
}
return TRUE;
} /* Else not a symlink */
chmod(destpath, new_mode & ~S_IFMT);