Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/miscutils/devfsd.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/miscutils/devfsd.c
r821 r1770 1 /* vi: set sw=4 ts=4: */ 2 /* 3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 4 */ 5 1 6 /* 2 7 devfsd implementation for busybox … … 50 55 */ 51 56 52 #include "busybox.h" 53 #include "xregex.h" 54 #include <unistd.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <stdarg.h> 58 #include <string.h> 59 #include <ctype.h> 60 #include <sys/stat.h> 61 #include <sys/types.h> 62 #include <sys/wait.h> 63 #include <sys/ioctl.h> 64 #include <sys/socket.h> 57 //#include <sys/wait.h> 58 //#include <sys/ioctl.h> 59 //#include <sys/socket.h> 65 60 #include <sys/un.h> 66 61 #include <dirent.h> 67 #include <fcntl.h>68 62 #include <syslog.h> 69 #include <signal.h>70 #include <errno.h>71 63 #include <sys/sysmacros.h> 64 #include "libbb.h" 65 #include "xregex.h" 72 66 73 67 … … 165 159 struct permissions_type 166 160 { 167 168 169 161 mode_t mode; 162 uid_t uid; 163 gid_t gid; 170 164 }; 171 165 172 166 struct execute_type 173 167 { 174 168 char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */ 175 169 }; 176 170 177 171 struct copy_type 178 172 { 179 180 173 const char *source; 174 const char *destination; 181 175 }; 182 176 183 177 struct action_type 184 178 { 185 186 179 unsigned int what; 180 unsigned int when; 187 181 }; 188 182 189 183 struct config_entry_struct 190 184 { 191 192 193 194 185 struct action_type action; 186 regex_t preg; 187 union 188 { 195 189 struct permissions_type permissions; 196 190 struct execute_type execute; 197 191 struct copy_type copy; 198 199 200 192 } 193 u; 194 struct config_entry_struct *next; 201 195 }; 202 196 203 197 struct get_variable_info 204 198 { 205 206 207 199 const struct devfsd_notify_struct *info; 200 const char *devname; 201 char devpath[STRING_LENGTH]; 208 202 }; 209 203 210 static void dir_operation(int , const char * , int, unsigned long* 204 static void dir_operation(int , const char * , int, unsigned long*); 211 205 static void service(struct stat statbuf, char *path); 212 static int st_expr_expand(char *, unsigned, const char *, const char *(*) 206 static int st_expr_expand(char *, unsigned, const char *, const char *(*)(const char *, void *), void *); 213 207 static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned); 214 static int mksymlink 215 static void read_config_file 216 static void process_config_line 217 static int do_servicing 218 static void service_name 219 static void action_permissions 220 static void action_execute 208 static int mksymlink(const char *oldpath, const char *newpath); 209 static void read_config_file(char *path, int optional, unsigned long *event_mask); 210 static void process_config_line(const char *, unsigned long *); 211 static int do_servicing(int, unsigned long); 212 static void service_name(const struct devfsd_notify_struct *); 213 static void action_permissions(const struct devfsd_notify_struct *, const struct config_entry_struct *); 214 static void action_execute(const struct devfsd_notify_struct *, const struct config_entry_struct *, 221 215 const regmatch_t *, unsigned); 222 static void action_modload 223 static void action_copy 216 static void action_modload(const struct devfsd_notify_struct *info, const struct config_entry_struct *entry); 217 static void action_copy(const struct devfsd_notify_struct *, const struct config_entry_struct *, 224 218 const regmatch_t *, unsigned); 225 static void action_compat 226 static void free_config 219 static void action_compat(const struct devfsd_notify_struct *, unsigned); 220 static void free_config(void); 227 221 static void restore(char *spath, struct stat source_stat, int rootlen); 228 static int copy_inode 229 static mode_t get_mode 230 static void signal_handler 231 static const char *get_variable 232 static int make_dir_tree 222 static int copy_inode(const char *, const struct stat *, mode_t, const char *, const struct stat *); 223 static mode_t get_mode(const char *); 224 static void signal_handler(int); 225 static const char *get_variable(const char *, void *); 226 static int make_dir_tree(const char *); 233 227 static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *, 234 const char *, const regmatch_t *, unsigned 235 static void expand_regexp (char *, size_t, const char *, const char *, const regmatch_t *, unsigned);228 const char *, const regmatch_t *, unsigned); 229 static void expand_regexp(char *, size_t, const char *, const char *, const regmatch_t *, unsigned); 236 230 static const char *expand_variable( char *, unsigned, unsigned *, const char *, 237 const char *(*) (const char *, void *), void *);238 static const char *get_variable_v2(const char *, const char *(*) 239 static char get_old_ide_name 240 static char *write_old_sd_name (char *, unsigned, unsigned,char *);231 const char *(*)(const char *, void *), void *); 232 static const char *get_variable_v2(const char *, const char *(*)(const char *, void *), void *); 233 static char get_old_ide_name(unsigned , unsigned); 234 static char *write_old_sd_name(char *, unsigned, unsigned, const char *); 241 235 242 236 /* busybox functions */ 243 static void msg_logger(int pri, const char * fmt, ... )__attribute__ ((format (printf, 2, 3))); 244 static void msg_logger_and_die(int pri, const char * fmt, ... )__attribute__ ((noreturn, format (printf, 2, 3))); 245 static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag); 246 static void fork_and_execute(int die, char *arg0, char **arg ); 247 static int get_uid_gid ( int, const char *); 248 static void safe_memcpy( char * dest, const char * src, int len); 249 static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr); 250 static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr); 237 static int get_uid_gid(int flag, const char *string); 238 static void safe_memcpy(char * dest, const char * src, int len); 239 static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr); 240 static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr); 251 241 252 242 /* Structs and vars */ 253 243 static struct config_entry_struct *first_config = NULL; 254 244 static struct config_entry_struct *last_config = NULL; 255 static c onst char *mount_point = NULL;245 static char *mount_point = NULL; 256 246 static volatile int caught_signal = FALSE; 257 247 static volatile int caught_sighup = FALSE; 258 static struct initial_symlink_struct 259 { 260 char *dest; 261 char *name; 262 } initial_symlinks[] = 263 { 264 {"/proc/self/fd", "fd"}, 265 {"fd/0", "stdin"}, 266 {"fd/1", "stdout"}, 267 {"fd/2", "stderr"}, 268 {NULL, NULL}, 248 static struct initial_symlink_struct { 249 const char *dest; 250 const char *name; 251 } initial_symlinks[] = { 252 {"/proc/self/fd", "fd"}, 253 {"fd/0", "stdin"}, 254 {"fd/1", "stdout"}, 255 {"fd/2", "stderr"}, 256 {NULL, NULL}, 269 257 }; 270 258 271 static struct event_type 272 { 273 unsigned int type; /* The DEVFSD_NOTIFY_* value */ 274 const char *config_name; /* The name used in the config file */ 275 } event_types[] = 276 { 277 {DEVFSD_NOTIFY_REGISTERED, "REGISTER"}, 278 {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"}, 279 {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"}, 280 {DEVFSD_NOTIFY_CLOSE, "CLOSE"}, 281 {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"}, 282 {DEVFSD_NOTIFY_CHANGE, "CHANGE"}, 283 {DEVFSD_NOTIFY_CREATE, "CREATE"}, 284 {DEVFSD_NOTIFY_DELETE, "DELETE"}, 285 {0xffffffff, NULL} 259 static struct event_type { 260 unsigned int type; /* The DEVFSD_NOTIFY_* value */ 261 const char *config_name; /* The name used in the config file */ 262 } event_types[] = { 263 {DEVFSD_NOTIFY_REGISTERED, "REGISTER"}, 264 {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"}, 265 {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"}, 266 {DEVFSD_NOTIFY_CLOSE, "CLOSE"}, 267 {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"}, 268 {DEVFSD_NOTIFY_CHANGE, "CHANGE"}, 269 {DEVFSD_NOTIFY_CREATE, "CREATE"}, 270 {DEVFSD_NOTIFY_DELETE, "DELETE"}, 271 {0xffffffff, NULL} 286 272 }; 287 273 288 274 /* Busybox messages */ 289 275 290 static const char * const bb_msg_proto_rev = "protocol revision"; 291 static const char * const bb_msg_bad_config = "bad %s config file: %s"; 292 static const char * const bb_msg_small_buffer = "buffer too small"; 293 static const char * const bb_msg_variable_not_found = "variable: %s not found"; 294 295 /* Busybox functions */ 296 static void msg_logger(int pri, const char * fmt, ... ) 297 { 298 va_list ap; 299 int ret; 300 301 va_start(ap, fmt); 302 ret = access ("/dev/log", F_OK); 303 if (ret == 0) { 304 openlog(bb_applet_name, 0, LOG_DAEMON); 305 vsyslog( pri , fmt, ap); 306 /* Man: A trailing newline is added when needed. */ 307 closelog(); 308 } 309 /* ENABLE_DEVFSD_VERBOSE is always enabled if msg_logger is used */ 310 if ((ENABLE_DEVFSD_VERBOSE && ret) || ENABLE_DEBUG) { 311 bb_error_msg(fmt, ap); 312 } 313 va_end(ap); 314 } 315 316 static void msg_logger_and_die(int pri, const char* fmt, ...) 317 { 318 va_list ap; 319 320 va_start(ap, fmt); 321 msg_logger(pri, fmt, ap); 322 va_end(ap); 323 exit(EXIT_FAILURE); 324 } 276 static const char bb_msg_proto_rev[] ALIGN1 = "protocol revision"; 277 static const char bb_msg_bad_config[] ALIGN1 = "bad %s config file: %s"; 278 static const char bb_msg_small_buffer[] ALIGN1 = "buffer too small"; 279 static const char bb_msg_variable_not_found[] ALIGN1 = "variable: %s not found"; 325 280 326 281 /* Busybox stuff */ 327 #if defined(CONFIG_DEVFSD_VERBOSE) || defined(CONFIG_DEBUG)328 #define devfsd_error_msg(fmt, args...) bb_error_msg(fmt, ## args)329 #define devfsd_perror_msg_and_die(fmt, args...) bb_perror_msg_and_die(fmt, ## args)330 #define devfsd_error_msg_and_die(fmt, args...)bb_error_msg_and_die(fmt, ## args)331 # if defined(CONFIG_DEBUG)332 #define debug_msg_logger(x, fmt, args...) msg_logger(x,fmt, ## args)282 #if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG 283 #define info_logger(p, fmt, args...) bb_info_msg(fmt, ## args) 284 #define msg_logger(p, fmt, args...) bb_error_msg(fmt, ## args) 285 #define msg_logger_and_die(p, fmt, args...) bb_error_msg_and_die(fmt, ## args) 286 #define error_logger(p, fmt, args...) bb_perror_msg(fmt, ## args) 287 #define error_logger_and_die(p, fmt, args...) bb_perror_msg_and_die(fmt, ## args) 333 288 #else 334 #define debug_msg_logger(x, fmt, args...) 335 #endif 336 #else 337 #define debug_msg_logger(x, fmt, args...) 289 #define info_logger(p, fmt, args...) 338 290 #define msg_logger(p, fmt, args...) 339 291 #define msg_logger_and_die(p, fmt, args...) exit(1) 340 #define devfsd_perror_msg_and_die(fmt, args...) exit(1) 341 #define devfsd_error_msg_and_die(fmt, args...) exit(1) 342 #define devfsd_error_msg(fmt, args...) 292 #define error_logger(p, fmt, args...) 293 #define error_logger_and_die(p, fmt, args...) exit(1) 343 294 #endif 344 295 345 static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag) 346 { 347 if (ioctl (fd, request, event_mask_flag) == -1) 348 msg_logger_and_die(LOG_ERR, "ioctl"); 349 } 350 351 static void fork_and_execute(int die, char *arg0, char **arg ) 352 { 353 switch ( fork () ) 354 { 355 case 0: 356 /* Child */ 357 break; 358 case -1: 359 /* Parent: Error : die or return */ 360 msg_logger(LOG_ERR,(char *) bb_msg_memory_exhausted); 361 if(die) 362 exit(EXIT_FAILURE); 363 return; 364 default: 365 /* Parent : ok : return or exit */ 366 if(arg0 != NULL) 367 { 368 wait (NULL); 369 return; 370 } 371 exit (EXIT_SUCCESS); 372 } 373 /* Child : if arg0 != NULL do execvp */ 374 if(arg0 != NULL ) 375 { 376 execvp (arg0, arg); 377 msg_logger_and_die(LOG_ERR, "execvp"); 378 } 379 } 380 381 static void safe_memcpy( char *dest, const char *src, int len) 382 { 383 memcpy (dest , src , len ); 296 static void safe_memcpy(char *dest, const char *src, int len) 297 { 298 memcpy(dest , src, len); 384 299 dest[len] = '\0'; 385 300 } 386 301 387 static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr) 388 { 389 if( d[n - 4]=='d' && d[n - 3]=='i' && d[n - 2]=='s' && d[n - 1]=='c') 390 return (2 + addendum); 391 else if( d[n - 2]=='c' && d[n - 1]=='d') 392 return (3 + addendum); 393 else if(ptr[0]=='p' && ptr[1]=='a' && ptr[2]=='r' && ptr[3]=='t') 394 return (4 + addendum); 395 else if( ptr[n - 2]=='m' && ptr[n - 1]=='t') 396 return (5 + addendum); 397 else 398 return 0; 302 static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr) 303 { 304 if (d[n - 4] == 'd' && d[n - 3] == 'i' && d[n - 2] == 's' && d[n - 1] == 'c') 305 return 2 + addendum; 306 if (d[n - 2] == 'c' && d[n - 1] == 'd') 307 return 3 + addendum; 308 if (ptr[0] == 'p' && ptr[1] == 'a' && ptr[2] == 'r' && ptr[3] == 't') 309 return 4 + addendum; 310 if (ptr[n - 2] == 'm' && ptr[n - 1] == 't') 311 return 5 + addendum; 312 return 0; 399 313 } 400 314 401 static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr) 402 { 403 if(d[0]=='s' && d[1]=='c' && d[2]=='s' && d[3]=='i' && d[4]=='/') 404 { 405 if( d[n - 7]=='g' && d[n - 6]=='e' && d[n - 5]=='n' && 406 d[n - 4]=='e' && d[n - 3]=='r' && d[n - 2]=='i' && 407 d[n - 1]=='c' ) 315 static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr) 316 { 317 if (d[0] == 's' && d[1] == 'c' && d[2] == 's' && d[3] == 'i' && d[4] == '/') { 318 if (d[n - 7] == 'g' && d[n - 6] == 'e' && d[n - 5] == 'n' 319 && d[n - 4] == 'e' && d[n - 3] == 'r' && d[n - 2] == 'i' && d[n - 1] == 'c' 320 ) 408 321 return 1; 409 322 return scan_dev_name_common(d, n, 0, ptr); 410 323 } 411 else if(d[0]=='i' && d[1]=='d' && d[2]=='e' && d[3]=='/' &&412 d[4]=='h' && d[5]=='o' && d[6]=='s' && d[7]=='t')413 {324 if (d[0] == 'i' && d[1] == 'd' && d[2] == 'e' && d[3] == '/' 325 && d[4] == 'h' && d[5] == 'o' && d[6] == 's' && d[7] == 't' 326 ) 414 327 return scan_dev_name_common(d, n, 4, ptr); 415 } 416 else if(d[0]=='s' && d[1]=='b' && d[2]=='p' && d[3]=='/') 417 { 328 if (d[0] == 's' && d[1] == 'b' && d[2] == 'p' && d[3] == '/') 418 329 return 10; 419 } 420 else if(d[0]=='v' && d[1]=='c' && d[2]=='c' && d[3]=='/') 421 { 330 if (d[0] == 'v' && d[1] == 'c' && d[2] == 'c' && d[3] == '/') 422 331 return 11; 423 } 424 else if(d[0]=='p' && d[1]=='t' && d[2]=='y' && d[3]=='/') 425 { 332 if (d[0] == 'p' && d[1] == 't' && d[2] == 'y' && d[3] == '/') 426 333 return 12; 427 }428 334 return 0; 429 335 } … … 431 337 /* Public functions follow */ 432 338 433 int devfsd_main (int argc, char **argv) 339 int devfsd_main(int argc, char **argv); 340 int devfsd_main(int argc, char **argv) 434 341 { 435 342 int print_version = FALSE; … … 445 352 bb_show_usage(); 446 353 447 for (count = 2; count < argc; ++count) 448 { 449 if(argv[count][0] == '-') 450 { 451 if(argv[count][1]=='v' && !argv[count][2]) /* -v */ 452 print_version = TRUE; 453 else if(ENABLE_DEVFSD_FG_NP && argv[count][1]=='f' 454 && argv[count][2]=='g' && !argv[count][3]) /* -fg */ 455 do_daemon = FALSE; 456 else if(ENABLE_DEVFSD_FG_NP && argv[count][1]=='n' 457 && argv[count][2]=='p' && !argv[count][3]) /* -np */ 458 no_polling = TRUE; 354 for (count = 2; count < argc; ++count) { 355 if (argv[count][0] == '-') { 356 if (argv[count][1] == 'v' && !argv[count][2]) /* -v */ 357 print_version = TRUE; 358 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'f' 359 && argv[count][2] == 'g' && !argv[count][3]) /* -fg */ 360 do_daemon = FALSE; 361 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'n' 362 && argv[count][2] == 'p' && !argv[count][3]) /* -np */ 363 no_polling = TRUE; 459 364 else 460 365 bb_show_usage(); … … 462 367 } 463 368 464 /* strip last / from mount point, so we don't need to check for it later */ 465 while( argv[1][1]!='\0' && argv[1][strlen(argv[1])-1] == '/' ) 466 argv[1][strlen(argv[1]) -1] = '\0'; 467 468 mount_point = argv[1]; 469 470 if (chdir (mount_point) != 0) 471 devfsd_perror_msg_and_die(mount_point); 472 473 fd = bb_xopen (".devfsd", O_RDONLY); 474 475 if (fcntl (fd, F_SETFD, FD_CLOEXEC) != 0) 476 devfsd_perror_msg_and_die("FD_CLOEXEC"); 477 478 if (ioctl (fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev) == -1) 479 msg_logger_and_die(LOG_ERR, "ioctl"); 369 mount_point = bb_simplify_path(argv[1]); 370 371 xchdir(mount_point); 372 373 fd = xopen(".devfsd", O_RDONLY); 374 375 if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) 376 bb_perror_msg_and_die("FD_CLOEXEC"); 377 378 xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev); 480 379 481 380 /*setup initial entries */ 482 483 symlink 381 for (curr = initial_symlinks; curr->dest != NULL; ++curr) 382 symlink(curr->dest, curr->name); 484 383 485 384 /* NB: The check for CONFIG_FILE is done in read_config_file() */ 486 385 487 if ( print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev) ) 488 { 489 bb_printf( "%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n", 490 bb_applet_name,DEVFSD_VERSION,bb_msg_proto_rev, 491 DEVFSD_PROTOCOL_REVISION_DAEMON,bb_msg_proto_rev, proto_rev); 386 if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) { 387 printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n", 388 applet_name, DEVFSD_VERSION, bb_msg_proto_rev, 389 DEVFSD_PROTOCOL_REVISION_DAEMON, bb_msg_proto_rev, proto_rev); 492 390 if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev) 493 bb_error_msg_and_die( "%s mismatch!",bb_msg_proto_rev);391 bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev); 494 392 exit(EXIT_SUCCESS); /* -v */ 495 393 } 496 /* Tell kernel we are special 497 do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, 0);498 499 sigemptyset 394 /* Tell kernel we are special(i.e. we get to see hidden entries) */ 395 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0); 396 397 sigemptyset(&new_action.sa_mask); 500 398 new_action.sa_flags = 0; 501 399 502 400 /* Set up SIGHUP and SIGUSR1 handlers */ 503 401 new_action.sa_handler = signal_handler; 504 if (sigaction (SIGHUP, &new_action, NULL) != 0 || sigaction (SIGUSR1, &new_action, NULL) != 0)505 devfsd_error_msg_and_die("sigaction");506 507 bb_printf("%s v%s started for %s\n",bb_applet_name, DEVFSD_VERSION, mount_point);402 if (sigaction(SIGHUP, &new_action, NULL) != 0 || sigaction(SIGUSR1, &new_action, NULL) != 0) 403 bb_error_msg_and_die("sigaction"); 404 405 printf("%s v%s started for %s\n",applet_name, DEVFSD_VERSION, mount_point); 508 406 509 407 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */ 510 umask 511 read_config_file (CONFIG_FILE, FALSE, &event_mask);408 umask(0); 409 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask); 512 410 /* Do the scan before forking, so that boot scripts see the finished product */ 513 dir_operation(SERVICE, mount_point,0,NULL);411 dir_operation(SERVICE, mount_point, 0, NULL); 514 412 515 413 if (ENABLE_DEVFSD_FG_NP && no_polling) 516 exit (0); 517 if (do_daemon) 518 { 414 exit(0); 415 416 if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG) 417 logmode = LOGMODE_BOTH; 418 else if (do_daemon == TRUE) 419 logmode = LOGMODE_SYSLOG; 420 /* This is the default */ 421 /*else 422 logmode = LOGMODE_STDIO; */ 423 424 if (do_daemon) { 519 425 /* Release so that the child can grab it */ 520 do_ioctl_and_die(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0); 521 fork_and_execute(DIE, NULL, NULL); 522 setsid (); /* Prevent hangups and become pgrp leader */ 523 } else if(ENABLE_DEVFSD_FG_NP) { 524 setpgid (0, 0); /* Become process group leader */ 525 } 526 527 while (TRUE) 528 { 529 do_scan = do_servicing (fd, event_mask); 530 531 free_config (); 532 read_config_file (CONFIG_FILE, FALSE, &event_mask); 426 xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0); 427 bb_daemonize_or_rexec(0, argv); 428 } else if (ENABLE_DEVFSD_FG_NP) { 429 setpgid(0, 0); /* Become process group leader */ 430 } 431 432 while (TRUE) { 433 do_scan = do_servicing(fd, event_mask); 434 435 free_config(); 436 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask); 533 437 if (do_scan) 534 dir_operation(SERVICE,mount_point,0,NULL); 535 } 438 dir_operation(SERVICE, mount_point, 0, NULL); 439 } 440 if (ENABLE_FEATURE_CLEAN_UP) free(mount_point); 536 441 } /* End Function main */ 537 442 … … 539 444 /* Private functions follow */ 540 445 541 static void read_config_file 446 static void read_config_file(char *path, int optional, unsigned long *event_mask) 542 447 /* [SUMMARY] Read a configuration database. 543 448 <path> The path to read the database from. If this is a directory, all 544 entries in that directory will be read 449 entries in that directory will be read(except hidden entries). 545 450 <optional> If TRUE, the routine will silently ignore a missing config file. 546 451 <event_mask> The event mask is written here. This is not initialised. … … 551 456 FILE *fp; 552 457 char buf[STRING_LENGTH]; 553 char *line=NULL; 554 555 debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, path); 556 557 if (stat (path, &statbuf) == 0 ) 558 { 458 char *line = NULL; 459 char *p; 460 461 if (stat(path, &statbuf) == 0) { 559 462 /* Don't read 0 length files: ignored */ 560 /*if ( statbuf.st_size == 0)463 /*if (statbuf.st_size == 0) 561 464 return;*/ 562 if ( S_ISDIR (statbuf.st_mode) ) 563 { 564 /* strip last / from dirname so we don't need to check for it later */ 565 while( path && path[1]!='\0' && path[strlen(path)-1] == '/') 566 path[strlen(path) -1] = '\0'; 567 568 dir_operation(READ_CONFIG, path, 0, event_mask); 465 if (S_ISDIR(statbuf.st_mode)) { 466 p = bb_simplify_path(path); 467 dir_operation(READ_CONFIG, p, 0, event_mask); 468 free(p); 569 469 return; 570 470 } 571 if ( ( fp = fopen (path, "r") ) != NULL ) 572 { 573 while (fgets (buf, STRING_LENGTH, fp) != NULL) 574 { 471 if ((fp = fopen(path, "r")) != NULL) { 472 while (fgets(buf, STRING_LENGTH, fp) != NULL) { 575 473 /* Skip whitespace */ 576 for (line = buf; isspace (*line); ++line)577 /*VOID*/;578 if (line[0] == '\0' || line[0] == '#' 474 line = buf; 475 line = skip_whitespace(line); 476 if (line[0] == '\0' || line[0] == '#') 579 477 continue; 580 process_config_line 478 process_config_line(line, event_mask); 581 479 } 582 fclose 480 fclose(fp); 583 481 } else { 584 482 goto read_config_file_err; … … 586 484 } else { 587 485 read_config_file_err: 588 if(optional == 0 && errno == ENOENT) 589 msg_logger_and_die(LOG_ERR, "read config file: %s: %m", path); 590 } 591 return; 486 if (optional == 0 && errno == ENOENT) 487 error_logger_and_die(LOG_ERR, "read config file: %s", path); 488 } 592 489 } /* End Function read_config_file */ 593 490 594 static void process_config_line 491 static void process_config_line(const char *line, unsigned long *event_mask) 595 492 /* [SUMMARY] Process a line from a configuration file. 596 493 <line> The configuration line. … … 604 501 char when[STRING_LENGTH], what[STRING_LENGTH]; 605 502 char name[STRING_LENGTH]; 606 c har * msg="";503 const char *msg = ""; 607 504 char *ptr; 608 505 int i; 609 506 610 507 /* !!!! Only Uppercase Keywords in devsfd.conf */ 611 static const char *const options[] = { 612 "CLEAR_CONFIG", "INCLUDE", "OPTIONAL_INCLUDE", 613 "RESTORE", "PERMISSIONS", "MODLOAD", "EXECUTE", 614 "COPY", "IGNORE", "MKOLDCOMPAT", "MKNEWCOMPAT", 615 "RMOLDCOMPAT", "RMNEWCOMPAT", 0 616 }; 617 618 debug_msg_logger(LOG_INFO, __FUNCTION__); 619 620 for (count = 0; count < MAX_ARGS; ++count) p[count][0] = '\0'; 621 num_args = sscanf (line, "%s %s %s %s %s %s %s %s %s %s", 508 static const char options[] ALIGN1 = 509 "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0" 510 "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0" 511 "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0" 512 "RMOLDCOMPAT\0""RMNEWCOMPAT\0"; 513 514 for (count = 0; count < MAX_ARGS; ++count) 515 p[count][0] = '\0'; 516 num_args = sscanf(line, "%s %s %s %s %s %s %s %s %s %s", 622 517 when, name, what, 623 518 p[0], p[1], p[2], p[3], p[4], p[5], p[6]); 624 519 625 i = compare_string_array(options, when ); 626 627 /*"CLEAR_CONFIG"*/ 628 if( i == 0) 629 { 630 free_config (); 520 i = index_in_strings(options, when); 521 522 /* "CLEAR_CONFIG" */ 523 if (i == 0) { 524 free_config(); 631 525 *event_mask = 0; 632 526 return; 633 527 } 634 528 635 if ( 529 if (num_args < 2) 636 530 goto process_config_line_err; 637 531 638 532 /* "INCLUDE" & "OPTIONAL_INCLUDE" */ 639 if( i == 1 || i == 2 ) 640 { 641 st_expr_expand (name, STRING_LENGTH, name, get_variable, NULL ); 642 msg_logger(LOG_INFO, "%sinclude: %s",(toupper (when[0]) == 'I') ? "": "optional_", name); 643 read_config_file (name, (toupper (when[0]) == 'I') ? FALSE : TRUE, event_mask); 533 if (i == 1 || i == 2) { 534 st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL); 535 info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name); 536 read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask); 644 537 return; 645 538 } 646 539 /* "RESTORE" */ 647 if( i == 3) 648 { 649 dir_operation(RESTORE,name, strlen (name),NULL); 540 if (i == 3) { 541 dir_operation(RESTORE, name, strlen(name),NULL); 650 542 return; 651 543 } … … 653 545 goto process_config_line_err; 654 546 655 new = xmalloc (sizeof *new); 656 memset (new, 0, sizeof *new); 657 658 for (count = 0; event_types[count].config_name != NULL; ++count) 659 { 660 if (strcasecmp (when, event_types[count].config_name) != 0) 547 new = xzalloc(sizeof *new); 548 549 for (count = 0; event_types[count].config_name != NULL; ++count) { 550 if (strcasecmp(when, event_types[count].config_name) != 0) 661 551 continue; 662 552 new->action.when = event_types[count].type; 663 553 break; 664 554 } 665 if (event_types[count].config_name == NULL) 666 { 667 msg="WHEN in"; 555 if (event_types[count].config_name == NULL) { 556 msg = "WHEN in"; 668 557 goto process_config_line_err; 669 558 } 670 559 671 i = compare_string_array(options, what ); 672 673 switch(i) 674 { 560 i = index_in_strings(options, what); 561 562 switch (i) { 675 563 case 4: /* "PERMISSIONS" */ 676 564 new->action.what = AC_PERMISSIONS; 677 565 /* Get user and group */ 678 if ( ( ptr = strchr (p[0], '.') ) == NULL ) 679 { 680 msg="UID.GID"; 566 if ((ptr = strchr(p[0], '.')) == NULL) { 567 msg = "UID.GID"; 681 568 goto process_config_line_err; /*"missing '.' in UID.GID"*/ 682 569 } 683 570 684 571 *ptr++ = '\0'; 685 new->u.permissions.uid = get_uid_gid 686 new->u.permissions.gid = get_uid_gid 572 new->u.permissions.uid = get_uid_gid(UID, p[0]); 573 new->u.permissions.gid = get_uid_gid(GID, ptr); 687 574 /* Get mode */ 688 new->u.permissions.mode = get_mode 575 new->u.permissions.mode = get_mode(p[1]); 689 576 break; 690 577 case 5: /* MODLOAD */ 691 /*This action will pass "/dev/$devname" 578 /*This action will pass "/dev/$devname"(i.e. "/dev/" prefixed to 692 579 the device name) to the module loading facility. In addition, 693 580 the /etc/modules.devfs configuration file is used.*/ … … 700 587 701 588 for (count = 0; count < num_args; ++count) 702 new->u.execute.argv[count] = bb_xstrdup(p[count]);589 new->u.execute.argv[count] = xstrdup(p[count]); 703 590 704 591 new->u.execute.argv[num_args] = NULL; … … 710 597 goto process_config_line_err; /* missing path and function in line */ 711 598 712 new->u.copy.source = bb_xstrdup(p[0]);713 new->u.copy.destination = bb_xstrdup(p[1]);599 new->u.copy.source = xstrdup(p[0]); 600 new->u.copy.destination = xstrdup(p[1]); 714 601 break; 715 602 case 8: /* IGNORE */ … … 730 617 break; 731 618 default: 732 msg = "WHAT in";619 msg = "WHAT in"; 733 620 goto process_config_line_err; 734 621 /*esac*/ 735 622 } /* switch (i) */ 736 623 737 xregcomp( 624 xregcomp(&new->preg, name, REG_EXTENDED); 738 625 739 626 *event_mask |= 1 << new->action.when; … … 745 632 last_config = new; 746 633 return; 747 process_config_line_err: 634 635 process_config_line_err: 748 636 msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line); 749 637 } /* End Function process_config_line */ 750 638 751 static int do_servicing 639 static int do_servicing(int fd, unsigned long event_mask) 752 640 /* [SUMMARY] Service devfs changes until a signal is received. 753 641 <fd> The open control file. … … 758 646 ssize_t bytes; 759 647 struct devfsd_notify_struct info; 760 unsigned long tmp_event_mask; 761 762 debug_msg_logger(LOG_INFO, __FUNCTION__); 763 764 /* Tell devfs what events we care about */ 765 tmp_event_mask = event_mask; 766 do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, tmp_event_mask); 767 while (!caught_signal) 768 { 648 649 /* (void*) cast is only in order to match prototype */ 650 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask); 651 while (!caught_signal) { 769 652 errno = 0; 770 bytes = read (fd,(char *) &info, sizeof info);653 bytes = read(fd,(char *) &info, sizeof info); 771 654 if (caught_signal) 772 655 break; /* Must test for this first */ … … 775 658 if (bytes < 1) 776 659 break; 777 service_name (&info); 778 } 779 if (caught_signal) 780 { 660 service_name(&info); 661 } 662 if (caught_signal) { 781 663 int c_sighup = caught_sighup; 782 664 783 665 caught_signal = FALSE; 784 666 caught_sighup = FALSE; 785 return (c_sighup);667 return c_sighup; 786 668 } 787 669 msg_logger_and_die(LOG_ERR, "read error on control file"); 788 670 } /* End Function do_servicing */ 789 671 790 static void service_name 672 static void service_name(const struct devfsd_notify_struct *info) 791 673 /* [SUMMARY] Service a single devfs change. 792 674 <info> The devfs change. … … 798 680 struct config_entry_struct *entry; 799 681 800 debug_msg_logger(LOG_INFO, __FUNCTION__);801 682 if (ENABLE_DEBUG && info->overrun_count > 0) 802 debug_msg_logger(LOG_ERR, "lost %u events", info->overrun_count);683 msg_logger(LOG_ERR, "lost %u events", info->overrun_count); 803 684 804 685 /* Discard lookups on "/dev/log" and "/dev/initctl" */ 805 if( info->type == DEVFSD_NOTIFY_LOOKUP && 806 ((info->devname[0]=='l' && info->devname[1]=='o' && 807 info->devname[2]=='g' && !info->devname[3]) || 808 ( info->devname[0]=='i' && info->devname[1]=='n' && 809 info->devname[2]=='i' && info->devname[3]=='t' && 810 info->devname[4]=='c' && info->devname[5]=='t' && 811 info->devname[6]=='l' && !info->devname[7]))) 812 return; 813 for (entry = first_config; entry != NULL; entry = entry->next) 814 { 686 if (info->type == DEVFSD_NOTIFY_LOOKUP 687 && ((info->devname[0] == 'l' && info->devname[1] == 'o' 688 && info->devname[2] == 'g' && !info->devname[3]) 689 || (info->devname[0] == 'i' && info->devname[1] == 'n' 690 && info->devname[2] == 'i' && info->devname[3] == 't' 691 && info->devname[4] == 'c' && info->devname[5] == 't' 692 && info->devname[6] == 'l' && !info->devname[7])) 693 ) 694 return; 695 696 for (entry = first_config; entry != NULL; entry = entry->next) { 815 697 /* First check if action matches the type, then check if name matches */ 816 if (info->type != entry->action.when || regexec (&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0 ) 698 if (info->type != entry->action.when 699 || regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0) 817 700 continue; 818 for (n = 0; 701 for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n) 819 702 /* VOID */; 820 703 821 debug_msg_logger(LOG_INFO, "%s: action.what %d", __FUNCTION__, entry->action.what); 822 823 switch (entry->action.what) 824 { 704 switch (entry->action.what) { 825 705 case AC_PERMISSIONS: 826 action_permissions 706 action_permissions(info, entry); 827 707 break; 828 708 case AC_MODLOAD: 829 if (ENABLE_DEVFSD_MODLOAD)830 action_modload 709 if (ENABLE_DEVFSD_MODLOAD) 710 action_modload(info, entry); 831 711 break; 832 712 case AC_EXECUTE: 833 action_execute 713 action_execute(info, entry, mbuf, n); 834 714 break; 835 715 case AC_COPY: 836 action_copy 716 action_copy(info, entry, mbuf, n); 837 717 break; 838 718 case AC_IGNORE: … … 843 723 case AC_RMOLDCOMPAT: 844 724 case AC_RMNEWCOMPAT: 845 action_compat 725 action_compat(info, entry->action.what); 846 726 break; 847 727 default: … … 851 731 } /* End Function service_name */ 852 732 853 static void action_permissions 733 static void action_permissions(const struct devfsd_notify_struct *info, 854 734 const struct config_entry_struct *entry) 855 735 /* [SUMMARY] Update permissions for a device entry. … … 861 741 struct stat statbuf; 862 742 863 debug_msg_logger(LOG_INFO, __FUNCTION__); 864 865 if ( stat (info->devname, &statbuf) != 0 || 866 chmod (info->devname,(statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0 || 867 chown (info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0) 868 { 869 msg_logger(LOG_ERR, "Can't chmod or chown: %s: %m",info->devname); 870 } 743 if (stat(info->devname, &statbuf) != 0 744 || chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0 745 || chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0 746 ) 747 error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname); 871 748 } /* End Function action_permissions */ 872 749 873 static void action_modload 750 static void action_modload(const struct devfsd_notify_struct *info, 874 751 const struct config_entry_struct *entry ATTRIBUTE_UNUSED) 875 752 /* [SUMMARY] Load a module. … … 880 757 { 881 758 char *argv[6]; 882 char device[STRING_LENGTH]; 883 884 argv[0] = MODPROBE; 885 argv[1] = MODPROBE_SWITCH_1; /* "-k" */ 886 argv[2] = MODPROBE_SWITCH_2; /* "-C" */ 887 argv[3] = CONFIG_MODULES_DEVFS; 888 argv[4] = device; 759 760 argv[0] = (char*)MODPROBE; 761 argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */ 762 argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */ 763 argv[3] = (char*)CONFIG_MODULES_DEVFS; 764 argv[4] = concat_path_file("/dev", info->devname); /* device */ 889 765 argv[5] = NULL; 890 766 891 snprintf (device, sizeof (device), "/dev/%s", info->devname); 892 debug_msg_logger(LOG_INFO, "%s: %s %s %s %s %s",__FUNCTION__, argv[0],argv[1],argv[2],argv[3],argv[4]); 893 fork_and_execute(DIE, argv[0], argv); 767 wait4pid(xspawn(argv)); 768 free(argv[4]); 894 769 } /* End Function action_modload */ 895 770 896 static void action_execute 771 static void action_execute(const struct devfsd_notify_struct *info, 897 772 const struct config_entry_struct *entry, 898 773 const regmatch_t *regexpr, unsigned int numexpr) … … 900 775 <info> The devfs change. 901 776 <entry> The config file entry. 902 <regexpr> The number of subexpression 777 <regexpr> The number of subexpression(start, end) offsets within the 903 778 device name. 904 779 <numexpr> The number of elements within <<regexpr>>. … … 911 786 char largv[MAX_ARGS + 1][STRING_LENGTH]; 912 787 913 debug_msg_logger(LOG_INFO ,__FUNCTION__);914 788 gv_info.info = info; 915 789 gv_info.devname = info->devname; 916 snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname); 917 for (count = 0; entry->u.execute.argv[count] != NULL; ++count) 918 { 919 expand_expression (largv[count], STRING_LENGTH, 790 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname); 791 for (count = 0; entry->u.execute.argv[count] != NULL; ++count) { 792 expand_expression(largv[count], STRING_LENGTH, 920 793 entry->u.execute.argv[count], 921 794 get_variable, &gv_info, 922 gv_info.devname, regexpr, numexpr 795 gv_info.devname, regexpr, numexpr); 923 796 argv[count] = largv[count]; 924 797 } 925 798 argv[count] = NULL; 926 fork_and_execute(NO_DIE, argv[0], argv);799 wait4pid(spawn(argv)); 927 800 } /* End Function action_execute */ 928 801 929 802 930 static void action_copy 803 static void action_copy(const struct devfsd_notify_struct *info, 931 804 const struct config_entry_struct *entry, 932 805 const regmatch_t *regexpr, unsigned int numexpr) … … 934 807 <info> The devfs change. 935 808 <entry> The config file entry. 936 <regexpr> This list of subexpression 809 <regexpr> This list of subexpression(start, end) offsets within the 937 810 device name. 938 811 <numexpr> The number of elements in <<regexpr>>. … … 946 819 int ret = 0; 947 820 948 debug_msg_logger(LOG_INFO, __FUNCTION__);949 950 821 dest_stat.st_mode = 0; 951 822 952 if ( (info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK (info->mode))823 if ((info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK(info->mode)) 953 824 return; 954 825 gv_info.info = info; 955 826 gv_info.devname = info->devname; 956 827 957 snprintf (gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);958 expand_expression 828 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname); 829 expand_expression(source, STRING_LENGTH, entry->u.copy.source, 959 830 get_variable, &gv_info, gv_info.devname, 960 831 regexpr, numexpr); 961 832 962 expand_expression 833 expand_expression(destination, STRING_LENGTH, entry->u.copy.destination, 963 834 get_variable, &gv_info, gv_info.devname, 964 835 regexpr, numexpr); 965 836 966 if ( !make_dir_tree (destination) || lstat(source, &source_stat) != 0)837 if (!make_dir_tree(destination) || lstat(source, &source_stat) != 0) 967 838 return; 968 lstat 839 lstat(destination, &dest_stat); 969 840 new_mode = source_stat.st_mode & ~S_ISVTX; 970 841 if (info->type == DEVFSD_NOTIFY_CREATE) 971 842 new_mode |= S_ISVTX; 972 else if ( (info->type == DEVFSD_NOTIFY_CHANGE) && (dest_stat.st_mode & S_ISVTX))843 else if ((info->type == DEVFSD_NOTIFY_CHANGE) &&(dest_stat.st_mode & S_ISVTX)) 973 844 new_mode |= S_ISVTX; 974 ret = copy_inode 845 ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat); 975 846 if (ENABLE_DEBUG && ret && (errno != EEXIST)) 976 debug_msg_logger(LOG_ERR, "copy_inode: %s to %s: %m", source, destination); 977 return; 847 error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination); 978 848 } /* End Function action_copy */ 979 849 980 static void action_compat 850 static void action_compat(const struct devfsd_notify_struct *info, unsigned int action) 981 851 /* [SUMMARY] Process a compatibility request. 982 852 <info> The devfs change. … … 988 858 const char *compat_name = NULL; 989 859 const char *dest_name = info->devname; 990 c har *ptr=NULL;860 const char *ptr; 991 861 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH]; 992 862 int mode, host, bus, target, lun; … … 1009 879 1010 880 /* First construct compatibility name */ 1011 switch (action) 1012 { 881 switch (action) { 1013 882 case AC_MKOLDCOMPAT: 1014 883 case AC_RMOLDCOMPAT: 1015 compat_name = get_old_name 884 compat_name = get_old_name(info->devname, info->namelen, compat_buf, info->major, info->minor); 1016 885 break; 1017 886 case AC_MKNEWCOMPAT: 1018 887 case AC_RMNEWCOMPAT: 1019 ptr = strrchr (info->devname, '/') + 1; 1020 i=scan_dev_name(info->devname, info->namelen, ptr); 1021 1022 debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i); 888 ptr = bb_basename(info->devname); 889 i = scan_dev_name(info->devname, info->namelen, ptr); 1023 890 1024 891 /* nothing found */ 1025 if (i==0 || i > 9)892 if (i == 0 || i > 9) 1026 893 return; 1027 894 1028 sscanf (info->devname +((i<6)?5:4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);1029 snprintf (dest_buf, sizeof (dest_buf), "../%s", info->devname + ((i>5)?4:0));895 sscanf(info->devname + ((i < 6) ? 5 : 4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun); 896 snprintf(dest_buf, sizeof(dest_buf), "../%s", info->devname + (( i > 5) ? 4 : 0)); 1030 897 dest_name = dest_buf; 1031 898 compat_name = compat_buf; … … 1033 900 1034 901 /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */ 1035 if ( i == 1 || i == 2 || i == 3 || i == 6 || i ==7)1036 sprintf (compat_buf, fmt[i], host, bus, target, lun);902 if (i == 1 || i == 2 || i == 3 || i == 6 || i ==7) 903 sprintf(compat_buf, fmt[i], host, bus, target, lun); 1037 904 1038 905 /* 4 == scsi/part 8 == ide/host/part */ 1039 if (i == 4 || i == 8)1040 sprintf ( compat_buf, fmt[i], host, bus, target, lun, atoi (ptr + 4));906 if (i == 4 || i == 8) 907 sprintf(compat_buf, fmt[i], host, bus, target, lun, atoi(ptr + 4)); 1041 908 1042 909 /* 5 == scsi/mt */ 1043 if( i == 5) 1044 { 910 if (i == 5) { 1045 911 rewind_ = info->devname[info->namelen - 1]; 1046 912 if (rewind_ != 'n') 1047 913 rewind_ = '\0'; 1048 914 mode=0; 1049 if (ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)915 if (ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/) 1050 916 mode = ptr[2] - 107; /* 1 or 2 */ 1051 if (ptr[2] == 'a')917 if (ptr[2] == 'a') 1052 918 mode = 3; 1053 sprintf (compat_buf, fmt[i], host, bus, target, lun, mode, rewind_);919 sprintf(compat_buf, fmt[i], host, bus, target, lun, mode, rewind_); 1054 920 } 1055 921 1056 922 /* 9 == ide/host/mt */ 1057 if ( i == 9)1058 snprintf (compat_buf, sizeof(compat_buf), fmt[i], host, bus, target, lun, ptr + 2);923 if (i == 9) 924 snprintf(compat_buf, sizeof(compat_buf), fmt[i], host, bus, target, lun, ptr + 2); 1059 925 /* esac */ 1060 } /* switch (action) */1061 1062 if (compat_name == NULL)926 } /* switch (action) */ 927 928 if (compat_name == NULL) 1063 929 return; 1064 930 1065 debug_msg_logger( LOG_INFO, "%s: %s", __FUNCTION__, compat_name);1066 1067 931 /* Now decide what to do with it */ 1068 switch (action) 1069 { 932 switch (action) { 1070 933 case AC_MKOLDCOMPAT: 1071 934 case AC_MKNEWCOMPAT: 1072 mksymlink 935 mksymlink(dest_name, compat_name); 1073 936 break; 1074 937 case AC_RMOLDCOMPAT: 1075 938 case AC_RMNEWCOMPAT: 1076 ret = unlink 939 ret = unlink(compat_name); 1077 940 if (ENABLE_DEBUG && ret) 1078 debug_msg_logger(LOG_ERR, "unlink: %s: %m", compat_name);941 error_logger(LOG_ERR, "unlink: %s", compat_name); 1079 942 break; 1080 943 /*esac*/ 1081 } /* switch (action) */944 } /* switch (action) */ 1082 945 } /* End Function action_compat */ 1083 946 1084 947 static void restore(char *spath, struct stat source_stat, int rootlen) 1085 948 { 1086 char dpath[STRING_LENGTH];949 char *dpath; 1087 950 struct stat dest_stat; 1088 951 1089 debug_msg_logger(LOG_INFO, __FUNCTION__);1090 1091 952 dest_stat.st_mode = 0; 1092 snprintf (dpath, sizeof dpath, "%s%s",mount_point, spath + rootlen);1093 lstat 1094 1095 if ( S_ISLNK (source_stat.st_mode) || (source_stat.st_mode & S_ISVTX))1096 copy_inode (dpath, &dest_stat,(source_stat.st_mode & ~S_ISVTX) , spath, &source_stat);1097 1098 if ( S_ISDIR (source_stat.st_mode))953 dpath = concat_path_file(mount_point, spath + rootlen); 954 lstat(dpath, &dest_stat); 955 free(dpath); 956 if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX)) 957 copy_inode(dpath, &dest_stat,(source_stat.st_mode & ~S_ISVTX) , spath, &source_stat); 958 959 if (S_ISDIR(source_stat.st_mode)) 1099 960 dir_operation(RESTORE, spath, rootlen,NULL); 1100 961 } 1101 962 1102 963 1103 static int copy_inode 964 static int copy_inode(const char *destpath, const struct stat *dest_stat, 1104 965 mode_t new_mode, 1105 966 const char *sourcepath, const struct stat *source_stat) … … 1119 980 char symlink_val[STRING_LENGTH]; 1120 981 1121 debug_msg_logger(LOG_INFO, __FUNCTION__); 1122 1123 if ( (source_stat->st_mode & S_IFMT) == (dest_stat->st_mode & S_IFMT) ) 1124 { 982 if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) { 1125 983 /* Same type */ 1126 if ( S_ISLNK (source_stat->st_mode) )1127 {1128 if (( source_len = readlink (sourcepath, source_link, STRING_LENGTH - 1) ) < 0 ||1129 ( dest_len = readlink (destpath , dest_link , STRING_LENGTH - 1) ) < 0)1130 return (FALSE);984 if (S_ISLNK(source_stat->st_mode)) { 985 if ((source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1)) < 0 986 || (dest_len = readlink(destpath , dest_link , STRING_LENGTH - 1)) < 0 987 ) 988 return FALSE; 1131 989 source_link[source_len] = '\0'; 1132 990 dest_link[dest_len] = '\0'; 1133 if ( (source_len != dest_len) || (strcmp (source_link, dest_link) != 0) ) 1134 { 1135 unlink (destpath); 1136 symlink (source_link, destpath); 991 if ((source_len != dest_len) || (strcmp(source_link, dest_link) != 0)) { 992 unlink(destpath); 993 symlink(source_link, destpath); 1137 994 } 1138 return (TRUE);995 return TRUE; 1139 996 } /* Else not a symlink */ 1140 chmod 1141 chown 1142 return (TRUE);997 chmod(destpath, new_mode & ~S_IFMT); 998 chown(destpath, source_stat->st_uid, source_stat->st_gid); 999 return TRUE; 1143 1000 } 1144 1001 /* Different types: unlink and create */ 1145 unlink (destpath); 1146 switch (source_stat->st_mode & S_IFMT) 1147 { 1002 unlink(destpath); 1003 switch (source_stat->st_mode & S_IFMT) { 1148 1004 case S_IFSOCK: 1149 if ( ( fd = socket (AF_UNIX, SOCK_STREAM, 0) ) < 0)1005 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) 1150 1006 break; 1151 1007 un_addr.sun_family = AF_UNIX; 1152 snprintf (un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath);1153 val = bind (fd, (struct sockaddr *) &un_addr,(int) sizeof un_addr);1154 close 1155 if (val != 0 || chmod 1008 snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath); 1009 val = bind(fd,(struct sockaddr *) &un_addr,(int) sizeof un_addr); 1010 close(fd); 1011 if (val != 0 || chmod(destpath, new_mode & ~S_IFMT) != 0) 1156 1012 break; 1157 1013 goto do_chown; 1158 1014 case S_IFLNK: 1159 if ( ( val = readlink (sourcepath, symlink_val, STRING_LENGTH - 1) ) < 0)1015 if ((val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1)) < 0) 1160 1016 break; 1161 1017 symlink_val[val] = '\0'; 1162 if (symlink 1163 return (TRUE);1018 if (symlink(symlink_val, destpath) == 0) 1019 return TRUE; 1164 1020 break; 1165 1021 case S_IFREG: 1166 if ( ( fd = open (destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT) ) < 0)1022 if ((fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT)) < 0) 1167 1023 break; 1168 close 1169 if (chmod 1024 close(fd); 1025 if (chmod(destpath, new_mode & ~S_IFMT) != 0) 1170 1026 break; 1171 1027 goto do_chown; … … 1173 1029 case S_IFCHR: 1174 1030 case S_IFIFO: 1175 if (mknod 1031 if (mknod(destpath, new_mode, source_stat->st_rdev) != 0) 1176 1032 break; 1177 1033 goto do_chown; 1178 1034 case S_IFDIR: 1179 if (mkdir 1035 if (mkdir(destpath, new_mode & ~S_IFMT) != 0) 1180 1036 break; 1181 1037 do_chown: 1182 if (chown 1183 return (TRUE);1038 if (chown(destpath, source_stat->st_uid, source_stat->st_gid) == 0) 1039 return TRUE; 1184 1040 /*break;*/ 1185 1041 } 1186 return (FALSE);1042 return FALSE; 1187 1043 } /* End Function copy_inode */ 1188 1044 1189 static void free_config 1045 static void free_config(void) 1190 1046 /* [SUMMARY] Free the configuration information. 1191 1047 [RETURNS] Nothing. … … 1195 1051 void *next; 1196 1052 1197 debug_msg_logger(LOG_INFO, __FUNCTION__); 1198 1199 for (c_entry = first_config; c_entry != NULL; c_entry = next) 1200 { 1053 for (c_entry = first_config; c_entry != NULL; c_entry = next) { 1201 1054 unsigned int count; 1202 1055 1203 1056 next = c_entry->next; 1204 regfree (&c_entry->preg); 1205 if (c_entry->action.what == AC_EXECUTE) 1206 { 1207 for (count = 0; count < MAX_ARGS; ++count) 1208 { 1057 regfree(&c_entry->preg); 1058 if (c_entry->action.what == AC_EXECUTE) { 1059 for (count = 0; count < MAX_ARGS; ++count) { 1209 1060 if (c_entry->u.execute.argv[count] == NULL) 1210 1061 break; 1211 free 1062 free(c_entry->u.execute.argv[count]); 1212 1063 } 1213 1064 } 1214 free 1065 free(c_entry); 1215 1066 } 1216 1067 first_config = NULL; … … 1218 1069 } /* End Function free_config */ 1219 1070 1220 static int get_uid_gid 1071 static int get_uid_gid(int flag, const char *string) 1221 1072 /* [SUMMARY] Convert a string to a UID or GID value. 1222 1073 <flag> "UID" or "GID". … … 1227 1078 struct passwd *pw_ent; 1228 1079 struct group *grp_ent; 1229 static c har *msg;1080 static const char *msg; 1230 1081 1231 1082 if (ENABLE_DEVFSD_VERBOSE) 1232 msg="user"; 1233 1234 debug_msg_logger(LOG_INFO, __FUNCTION__); 1235 1236 if(ENABLE_DEBUG && flag != UID && flag != GID) 1237 msg_logger_and_die(LOG_ERR,"%s: flag != UID && flag != GID", __FUNCTION__); 1238 1239 if ( isdigit (string[0]) || ( (string[0] == '-') && isdigit (string[1]) ) ) 1240 return atoi (string); 1241 1242 if ( flag == UID && ( pw_ent = getpwnam (string) ) != NULL ) 1243 return (pw_ent->pw_uid); 1244 1245 if ( flag == GID && ( grp_ent = getgrnam (string) ) != NULL ) 1246 return (grp_ent->gr_gid); 1247 else if(ENABLE_DEVFSD_VERBOSE) 1248 msg="group"; 1249 1250 if(ENABLE_DEVFSD_VERBOSE) 1083 msg = "user"; 1084 1085 if (isdigit(string[0]) ||((string[0] == '-') && isdigit(string[1]))) 1086 return atoi(string); 1087 1088 if (flag == UID && (pw_ent = getpwnam(string)) != NULL) 1089 return pw_ent->pw_uid; 1090 1091 if (flag == GID && (grp_ent = getgrnam(string)) != NULL) 1092 return grp_ent->gr_gid; 1093 else if (ENABLE_DEVFSD_VERBOSE) 1094 msg = "group"; 1095 1096 if (ENABLE_DEVFSD_VERBOSE) 1251 1097 msg_logger(LOG_ERR,"unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]); 1252 return (0);1098 return 0; 1253 1099 }/* End Function get_uid_gid */ 1254 1100 1255 static mode_t get_mode 1101 static mode_t get_mode(const char *string) 1256 1102 /* [SUMMARY] Convert a string to a mode value. 1257 1103 <string> The string. … … 1262 1108 int i; 1263 1109 1264 debug_msg_logger(LOG_INFO, __FUNCTION__); 1265 1266 if ( isdigit (string[0]) ) 1267 return strtoul (string, NULL, 8); 1268 if (strlen (string) != 9) 1110 if (isdigit(string[0])) 1111 return strtoul(string, NULL, 8); 1112 if (strlen(string) != 9) 1269 1113 msg_logger_and_die(LOG_ERR, "bad mode: %s", string); 1270 1114 1271 1115 mode = 0; 1272 i= S_IRUSR; 1273 while(i>0) 1274 { 1275 if(string[0]=='r'||string[0]=='w'||string[0]=='x') 1276 mode+=i; 1277 i=i/2; 1116 i = S_IRUSR; 1117 while (i > 0) { 1118 if (string[0] == 'r' || string[0] == 'w' || string[0] == 'x') 1119 mode += i; 1120 i = i / 2; 1278 1121 string++; 1279 1122 } 1280 return (mode);1123 return mode; 1281 1124 } /* End Function get_mode */ 1282 1125 1283 static void signal_handler (int sig) 1284 { 1285 debug_msg_logger(LOG_INFO, __FUNCTION__); 1286 1126 static void signal_handler(int sig) 1127 { 1287 1128 caught_signal = TRUE; 1288 1129 if (sig == SIGHUP) 1289 1130 caught_sighup = TRUE; 1290 1131 1291 msg_logger(LOG_INFO, "Caught signal %d", sig);1132 info_logger(LOG_INFO, "Caught signal %d", sig); 1292 1133 } /* End Function signal_handler */ 1293 1134 1294 static const char *get_variable (const char *variable, void *info) 1295 { 1135 static const char *get_variable(const char *variable, void *info) 1136 { 1137 static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */ 1138 1139 char hostname[STRING_LENGTH]; 1296 1140 struct get_variable_info *gv_info = info; 1297 static char hostname[STRING_LENGTH], sbuf[STRING_LENGTH]; 1298 const char *field_names[] = { "hostname", "mntpt", "devpath", "devname", 1299 "uid", "gid", "mode", hostname, mount_point, 1300 gv_info->devpath, gv_info->devname, 0 }; 1141 const char *field_names[] = { 1142 "hostname", "mntpt", "devpath", "devname", 1143 "uid", "gid", "mode", hostname, mount_point, 1144 gv_info->devpath, gv_info->devname, NULL 1145 }; 1301 1146 int i; 1302 1147 1303 debug_msg_logger(LOG_INFO, __FUNCTION__); 1304 1305 if (gethostname (hostname, STRING_LENGTH - 1) != 0) 1306 msg_logger_and_die(LOG_ERR, "gethostname: %m"); 1307 1148 if (gethostname(hostname, STRING_LENGTH - 1) != 0) 1308 1149 /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */ 1309 hostname[STRING_LENGTH - 1] = '\0'; 1310 1311 /* compare_string_array returns i>=0 */ 1312 i=compare_string_array(field_names, variable); 1313 1314 if ( i > 6 || i < 0 || (i > 1 && gv_info == NULL)) 1315 return (NULL); 1316 if( i >= 0 && i <= 3) 1317 { 1318 debug_msg_logger(LOG_INFO, "%s: i=%d %s", __FUNCTION__, i ,field_names[i+7]); 1319 return(field_names[i+7]); 1320 } 1321 1322 if(i == 4 ) 1323 sprintf (sbuf, "%u", gv_info->info->uid); 1324 else if(i == 5) 1325 sprintf (sbuf, "%u", gv_info->info->gid); 1326 else if(i == 6) 1327 sprintf (sbuf, "%o", gv_info->info->mode); 1328 1329 debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, sbuf); 1330 1331 return (sbuf); 1150 error_logger_and_die(LOG_ERR, "gethostname"); 1151 1152 hostname[STRING_LENGTH - 1] = '\0'; 1153 1154 /* index_in_str_array returns i>=0 */ 1155 i = index_in_str_array(field_names, variable); 1156 1157 if (i > 6 || i < 0 || (i > 1 && gv_info == NULL)) 1158 return NULL; 1159 if (i >= 0 && i <= 3) 1160 return field_names[i + 7]; 1161 1162 if (i == 4) 1163 sprintf(sbuf, "%u", gv_info->info->uid); 1164 else if (i == 5) 1165 sprintf(sbuf, "%u", gv_info->info->gid); 1166 else if (i == 6) 1167 sprintf(sbuf, "%o", gv_info->info->mode); 1168 return sbuf; 1332 1169 } /* End Function get_variable */ 1333 1170 … … 1336 1173 struct devfsd_notify_struct info; 1337 1174 1338 debug_msg_logger(LOG_INFO, __FUNCTION__); 1339 1340 memset (&info, 0, sizeof info); 1175 memset(&info, 0, sizeof info); 1341 1176 info.type = DEVFSD_NOTIFY_REGISTERED; 1342 1177 info.mode = statbuf.st_mode; 1343 info.major = major 1344 info.minor = minor 1178 info.major = major(statbuf.st_rdev); 1179 info.minor = minor(statbuf.st_rdev); 1345 1180 info.uid = statbuf.st_uid; 1346 1181 info.gid = statbuf.st_gid; 1347 snprintf (info.devname, sizeof (info.devname), "%s", path + strlen(mount_point) + 1);1348 info.namelen = strlen 1349 service_name 1350 if ( S_ISDIR (statbuf.st_mode))1351 dir_operation(SERVICE, path,0,NULL);1182 snprintf(info.devname, sizeof(info.devname), "%s", path + strlen(mount_point) + 1); 1183 info.namelen = strlen(info.devname); 1184 service_name(&info); 1185 if (S_ISDIR(statbuf.st_mode)) 1186 dir_operation(SERVICE, path, 0, NULL); 1352 1187 } 1353 1188 … … 1364 1199 DIR *dp; 1365 1200 struct dirent *de; 1366 char path[STRING_LENGTH]; 1367 1368 debug_msg_logger(LOG_INFO, __FUNCTION__); 1369 1370 if((dp = opendir( dir_name))==NULL) 1371 { 1372 debug_msg_logger(LOG_ERR, "opendir: %s: %m", dir_name); 1201 char *path; 1202 1203 if ((dp = warn_opendir(dir_name)) == NULL) 1373 1204 return; 1374 } 1375 1376 while ( (de = readdir (dp) ) != NULL ) 1377 { 1378 1379 if(de->d_name && *de->d_name == '.' && (!de->d_name[1] || (de->d_name[1] == '.' && !de->d_name[2]))) 1205 1206 while ((de = readdir(dp)) != NULL) { 1207 1208 if (de->d_name && DOT_OR_DOTDOT(de->d_name)) 1380 1209 continue; 1381 snprintf (path, sizeof (path), "%s/%s", dir_name, de->d_name); 1382 debug_msg_logger(LOG_ERR, "%s: %s", __FUNCTION__, path); 1383 1384 if (lstat (path, &statbuf) != 0) 1385 { 1386 debug_msg_logger(LOG_ERR, "%s: %s: %m", __FUNCTION__, path); 1387 continue; 1210 path = concat_path_file(dir_name, de->d_name); 1211 if (lstat(path, &statbuf) == 0) { 1212 switch (type) { 1213 case SERVICE: 1214 service(statbuf, path); 1215 break; 1216 case RESTORE: 1217 restore(path, statbuf, var); 1218 break; 1219 case READ_CONFIG: 1220 read_config_file(path, var, event_mask); 1221 break; 1222 } 1388 1223 } 1389 switch(type) 1390 { 1391 case SERVICE: 1392 service(statbuf,path); 1393 break; 1394 case RESTORE: 1395 restore(path, statbuf, var); 1396 break; 1397 case READ_CONFIG: 1398 read_config_file (path, var, event_mask); 1399 break; 1400 } 1401 } 1402 closedir (dp); 1224 free(path); 1225 } 1226 closedir(dp); 1403 1227 } /* End Function do_scan_and_service */ 1404 1228 1405 static int mksymlink 1229 static int mksymlink(const char *oldpath, const char *newpath) 1406 1230 /* [SUMMARY] Create a symlink, creating intervening directories as required. 1407 1231 <oldpath> The string contained in the symlink. … … 1410 1234 */ 1411 1235 { 1412 debug_msg_logger(LOG_INFO, __FUNCTION__); 1413 1414 if ( !make_dir_tree (newpath) ) 1415 return (-1); 1416 1417 if (symlink (oldpath, newpath) != 0) 1418 { 1236 if (!make_dir_tree(newpath)) 1237 return -1; 1238 1239 if (symlink(oldpath, newpath) != 0) { 1419 1240 if (errno != EEXIST) 1420 { 1421 debug_msg_logger(LOG_ERR, "%s: %s to %s: %m", __FUNCTION__, oldpath, newpath); 1422 return (-1); 1423 } 1424 } 1425 return (0); 1241 return -1; 1242 } 1243 return 0; 1426 1244 } /* End Function mksymlink */ 1427 1245 1428 1246 1429 static int make_dir_tree 1247 static int make_dir_tree(const char *path) 1430 1248 /* [SUMMARY] Creating intervening directories for a path as required. 1431 <path> The full pathname 1249 <path> The full pathname(including the leaf node). 1432 1250 [RETURNS] TRUE on success, else FALSE. 1433 1251 */ 1434 1252 { 1435 debug_msg_logger(LOG_INFO, __FUNCTION__); 1436 1437 if (bb_make_directory( dirname((char *)path), -1, FILEUTILS_RECUR )==-1) 1438 { 1439 debug_msg_logger(LOG_ERR, "%s: %s: %m",__FUNCTION__, path); 1440 return (FALSE); 1441 } 1442 return(TRUE); 1253 if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1) 1254 return FALSE; 1255 return TRUE; 1443 1256 } /* End Function make_dir_tree */ 1444 1257 … … 1466 1279 char temp[STRING_LENGTH]; 1467 1280 1468 debug_msg_logger(LOG_INFO, __FUNCTION__); 1469 1470 if ( !st_expr_expand (temp, STRING_LENGTH, input, get_variable_func, info) ) 1471 return (FALSE); 1472 expand_regexp (output, outsize, temp, devname, ex, numexp); 1473 return (TRUE); 1281 if (!st_expr_expand(temp, STRING_LENGTH, input, get_variable_func, info)) 1282 return FALSE; 1283 expand_regexp(output, outsize, temp, devname, ex, numexp); 1284 return TRUE; 1474 1285 } /* End Function expand_expression */ 1475 1286 1476 static void expand_regexp 1287 static void expand_regexp(char *output, size_t outsize, const char *input, 1477 1288 const char *devname, 1478 const regmatch_t *ex, unsigned int numex 1289 const regmatch_t *ex, unsigned int numex) 1479 1290 /* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9. 1480 1291 <output> The output expanded expression is written here. … … 1495 1306 int c = -1; 1496 1307 1497 debug_msg_logger(LOG_INFO, __FUNCTION__);1498 1499 1308 /* Guarantee NULL termination by writing an explicit '\0' character into 1500 1309 the very last byte */ … … 1504 1313 and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x 1505 1314 codes are deleted */ 1506 while ( (c != '\0') && (outsize != 0) ) 1507 { 1315 while ((c != '\0') && (outsize != 0)) { 1508 1316 c = *input; 1509 1317 ++input; 1510 if (c == '\\') 1511 { 1318 if (c == '\\') { 1512 1319 c = *input; 1513 1320 ++input; 1514 if (c != '\\') 1515 { 1516 if ((c >= '0') && (c <= last_exp)) 1517 { 1321 if (c != '\\') { 1322 if ((c >= '0') && (c <= last_exp)) { 1518 1323 const regmatch_t *subexp = ex + (c - '0'); 1519 1324 unsigned int sublen = subexp->rm_eo - subexp->rm_so; … … 1522 1327 if (sublen > outsize) 1523 1328 sublen = outsize; 1524 strncpy 1329 strncpy(output, devname + subexp->rm_so, sublen); 1525 1330 output += sublen; 1526 1331 outsize -= sublen; … … 1540 1345 struct translate_struct 1541 1346 { 1542 c har *match; /* The string to match to(up to length) */1543 c har *format; /* Format of output, "%s" takes data past match string,1544 NULL is effectively "%s" 1347 const char *match; /* The string to match to(up to length) */ 1348 const char *format; /* Format of output, "%s" takes data past match string, 1349 NULL is effectively "%s"(just more efficient) */ 1545 1350 }; 1546 1351 … … 1578 1383 }; 1579 1384 1580 const char *get_old_name 1385 const char *get_old_name(const char *devname, unsigned int namelen, 1581 1386 char *buffer, unsigned int major, unsigned int minor) 1582 1387 /* [SUMMARY] Translate a kernel-supplied name into an old name. … … 1590 1395 { 1591 1396 const char *compat_name = NULL; 1592 c har *ptr;1397 const char *ptr; 1593 1398 struct translate_struct *trans; 1594 1399 unsigned int i; … … 1616 1421 }; 1617 1422 1618 debug_msg_logger(LOG_INFO, __FUNCTION__); 1619 1620 for (trans = translate_table; trans->match != NULL; ++trans) 1621 { 1622 len = strlen (trans->match); 1623 1624 if (strncmp (devname, trans->match, len) == 0) 1625 { 1423 for (trans = translate_table; trans->match != NULL; ++trans) { 1424 len = strlen(trans->match); 1425 1426 if (strncmp(devname, trans->match, len) == 0) { 1626 1427 if (trans->format == NULL) 1627 return (devname + len);1628 sprintf 1629 return (buffer);1428 return devname + len; 1429 sprintf(buffer, trans->format, devname + len); 1430 return buffer; 1630 1431 } 1631 1432 } 1632 1433 1633 ptr = (strrchr (devname, '/') + 1);1434 ptr = bb_basename(devname); 1634 1435 i = scan_dev_name(devname, namelen, ptr); 1635 1436 1636 if (i > 0 && i < 13)1437 if (i > 0 && i < 13) 1637 1438 compat_name = buffer; 1638 1439 else 1639 1440 return NULL; 1640 1441 1641 debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i);1642 1643 1442 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */ 1644 if ( i == 1 || i == 3 || i == 10)1645 sprintf 1443 if (i == 1 || i == 3 || i == 10) 1444 sprintf(buffer, fmt[i], minor); 1646 1445 1647 1446 /* 2 ==scsi/disc, 4 == scsi/part */ 1648 if (i == 2 || i == 4)1649 compat_name = write_old_sd_name (buffer, major, minor,((i == 2)?"":(ptr + 4)));1447 if (i == 2 || i == 4) 1448 compat_name = write_old_sd_name(buffer, major, minor,((i == 2) ? "" : (ptr + 4))); 1650 1449 1651 1450 /* 5 == scsi/mt */ 1652 if( i == 5) 1653 { 1451 if (i == 5) { 1654 1452 mode = ptr[2]; 1655 1453 if (mode == 'n') 1656 1454 mode = '\0'; 1657 sprintf 1455 sprintf(buffer, fmt[i], minor & 0x1f, mode); 1658 1456 if (devname[namelen - 1] != 'n') 1659 1457 ++compat_name; 1660 1458 } 1661 1459 /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */ 1662 if ( i == 6 || i == 7 || i == 8)1460 if (i == 6 || i == 7 || i == 8) 1663 1461 /* last arg should be ignored for i == 6 or i== 7 */ 1664 sprintf (buffer, fmt[i] , get_old_ide_name(major, minor), ptr + 4);1462 sprintf(buffer, fmt[i] , get_old_ide_name(major, minor), ptr + 4); 1665 1463 1666 1464 /* 9 == ide/host/mt */ 1667 if ( i == 9)1668 sprintf 1465 if (i == 9) 1466 sprintf(buffer, fmt[i], ptr + 2, minor & 0x7f); 1669 1467 1670 1468 /* 11 == vcc/ */ 1671 if( i == 11 ) 1672 { 1673 sprintf (buffer, fmt[i], devname + 4); 1469 if (i == 11) { 1470 sprintf(buffer, fmt[i], devname + 4); 1674 1471 if (buffer[3] == '0') 1675 1472 buffer[3] = '\0'; 1676 1473 } 1677 1474 /* 12 == pty/ */ 1678 if( i == 12 ) 1679 { 1475 if (i == 12) { 1680 1476 pty1 = "pqrstuvwxyzabcde"; 1681 1477 pty2 = "0123456789abcdef"; 1682 indexx = atoi (devname + 5); 1683 sprintf (buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]); 1684 } 1685 1686 if(ENABLE_DEBUG && compat_name!=NULL) 1687 msg_logger(LOG_INFO, "%s: compat_name %s", __FUNCTION__, compat_name); 1688 1689 return (compat_name); 1478 indexx = atoi(devname + 5); 1479 sprintf(buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]); 1480 } 1481 return compat_name; 1690 1482 } /* End Function get_old_name */ 1691 1483 1692 static char get_old_ide_name 1484 static char get_old_ide_name(unsigned int major, unsigned int minor) 1693 1485 /* [SUMMARY] Get the old IDE name for a device. 1694 1486 <major> The major number for the device. … … 1697 1489 */ 1698 1490 { 1699 char letter='y'; /* 121 */ 1700 char c='a'; /* 97 */ 1701 int i=IDE0_MAJOR; 1702 1703 debug_msg_logger(LOG_INFO, __FUNCTION__); 1491 char letter = 'y'; /* 121 */ 1492 char c = 'a'; /* 97 */ 1493 int i = IDE0_MAJOR; 1704 1494 1705 1495 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */ 1706 1496 do { 1707 if( i==IDE0_MAJOR || i==IDE1_MAJOR || i==IDE2_MAJOR || 1708 i==IDE3_MAJOR || i==IDE4_MAJOR || i==IDE5_MAJOR || 1709 i==IDE6_MAJOR || i==IDE7_MAJOR || i==IDE8_MAJOR || 1710 i==IDE9_MAJOR ) 1711 { 1712 if((unsigned int)i==major) 1713 { 1714 letter=c; 1497 if (i == IDE0_MAJOR || i == IDE1_MAJOR || i == IDE2_MAJOR 1498 || i == IDE3_MAJOR || i == IDE4_MAJOR || i == IDE5_MAJOR 1499 || i == IDE6_MAJOR || i == IDE7_MAJOR || i == IDE8_MAJOR 1500 || i == IDE9_MAJOR 1501 ) { 1502 if ((unsigned int)i == major) { 1503 letter = c; 1715 1504 break; 1716 1505 } 1717 c +=2;1506 c += 2; 1718 1507 } 1719 1508 i++; 1720 } while (i<=IDE9_MAJOR);1509 } while (i <= IDE9_MAJOR); 1721 1510 1722 1511 if (minor > 63) 1723 1512 ++letter; 1724 return (letter);1513 return letter; 1725 1514 } /* End Function get_old_ide_name */ 1726 1515 1727 static char *write_old_sd_name 1516 static char *write_old_sd_name(char *buffer, 1728 1517 unsigned int major, unsigned int minor, 1729 c har *part)1518 const char *part) 1730 1519 /* [SUMMARY] Write the old SCSI disc name to a buffer. 1731 1520 <buffer> The buffer to write to. … … 1738 1527 unsigned int disc_index; 1739 1528 1740 debug_msg_logger(LOG_INFO, __FUNCTION__); 1741 1742 if (major == 8) 1743 { 1744 sprintf (buffer, "sd%c%s", 'a' + (minor >> 4), part); 1745 return (buffer); 1746 } 1747 if ( (major > 64) && (major < 72) ) 1748 { 1749 disc_index = ( (major - 64) << 4 ) + (minor >> 4); 1529 if (major == 8) { 1530 sprintf(buffer, "sd%c%s", 'a' + (minor >> 4), part); 1531 return buffer; 1532 } 1533 if ((major > 64) && (major < 72)) { 1534 disc_index = ((major - 64) << 4) +(minor >> 4); 1750 1535 if (disc_index < 26) 1751 sprintf 1536 sprintf(buffer, "sd%c%s", 'a' + disc_index, part); 1752 1537 else 1753 sprintf (buffer, "sd%c%c%s", 'a' + (disc_index / 26) - 1, 'a' + disc_index % 26,part);1754 return (buffer);1755 } 1756 return (NULL);1538 sprintf(buffer, "sd%c%c%s", 'a' +(disc_index / 26) - 1, 'a' + disc_index % 26, part); 1539 return buffer; 1540 } 1541 return NULL; 1757 1542 } /* End Function write_old_sd_name */ 1758 1543 … … 1762 1547 /*EXPERIMENTAL_FUNCTION*/ 1763 1548 1764 int st_expr_expand 1765 const char *(*get_variable_func) 1549 int st_expr_expand(char *output, unsigned int length, const char *input, 1550 const char *(*get_variable_func)(const char *variable, 1766 1551 void *info), 1767 1552 void *info) … … 1785 1570 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH]; 1786 1571 1787 debug_msg_logger(LOG_INFO, __FUNCTION__);1788 1789 1572 if (length > BUFFER_SIZE) 1790 1573 length = BUFFER_SIZE; 1791 for (; TRUE; ++input) 1792 { 1793 switch (ch = *input) 1794 { 1574 for (; TRUE; ++input) { 1575 switch (ch = *input) { 1795 1576 case '$': 1796 1577 /* Variable expansion */ 1797 input = expand_variable 1578 input = expand_variable(buffer, length, &out_pos, ++input, get_variable_func, info); 1798 1579 if (input == NULL) 1799 return (FALSE);1580 return FALSE; 1800 1581 break; 1801 1582 case '~': 1802 1583 /* Home directory expansion */ 1803 1584 ch = input[1]; 1804 if ( isspace (ch) || (ch == '/') || (ch == '\0') ) 1805 { 1585 if (isspace(ch) ||(ch == '/') ||(ch == '\0')) { 1806 1586 /* User's own home directory: leave separator for next time */ 1807 if ( ( env = getenv ("HOME") ) == NULL ) 1808 { 1809 msg_logger(LOG_INFO, bb_msg_variable_not_found, "HOME"); 1810 return (FALSE); 1587 if ((env = getenv("HOME")) == NULL) { 1588 info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME"); 1589 return FALSE; 1811 1590 } 1812 len = strlen 1591 len = strlen(env); 1813 1592 if (len + out_pos >= length) 1814 1593 goto st_expr_expand_out; 1815 memcpy 1594 memcpy(buffer + out_pos, env, len + 1); 1816 1595 out_pos += len; 1817 1596 continue; 1818 1597 } 1819 1598 /* Someone else's home directory */ 1820 for (ptr = ++input; !isspace 1821 /* VOID */ 1599 for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr) 1600 /* VOID */; 1822 1601 len = ptr - input; 1823 1602 if (len >= sizeof tmp) 1824 1603 goto st_expr_expand_out; 1825 safe_memcpy 1604 safe_memcpy(tmp, input, len); 1826 1605 input = ptr - 1; 1827 if ( ( pwent = getpwnam (tmp) ) == NULL ) 1828 { 1829 msg_logger(LOG_INFO, "no pwent for: %s", tmp); 1830 return (FALSE); 1606 if ((pwent = getpwnam(tmp)) == NULL) { 1607 info_logger(LOG_INFO, "no pwent for: %s", tmp); 1608 return FALSE; 1831 1609 } 1832 len = strlen 1610 len = strlen(pwent->pw_dir); 1833 1611 if (len + out_pos >= length) 1834 1612 goto st_expr_expand_out; 1835 memcpy 1613 memcpy(buffer + out_pos, pwent->pw_dir, len + 1); 1836 1614 out_pos += len; 1837 1615 break; … … 1842 1620 goto st_expr_expand_out; 1843 1621 buffer[out_pos++] = ch; 1844 if (ch == '\0') 1845 { 1846 memcpy (output, buffer, out_pos); 1847 return (TRUE); 1622 if (ch == '\0') { 1623 memcpy(output, buffer, out_pos); 1624 return TRUE; 1848 1625 } 1849 1626 break; … … 1851 1628 } 1852 1629 } 1853 return (FALSE);1630 return FALSE; 1854 1631 st_expr_expand_out: 1855 msg_logger(LOG_INFO, bb_msg_small_buffer);1856 return (FALSE);1632 info_logger(LOG_INFO, bb_msg_small_buffer); 1633 return FALSE; 1857 1634 } /* End Function st_expr_expand */ 1858 1635 … … 1860 1637 /* Private functions follow */ 1861 1638 1862 static const char *expand_variable 1639 static const char *expand_variable(char *buffer, unsigned int length, 1863 1640 unsigned int *out_pos, const char *input, 1864 const char *(*func) 1641 const char *(*func)(const char *variable, 1865 1642 void *info), 1866 1643 void *info) … … 1884 1661 char tmp[STRING_LENGTH]; 1885 1662 1886 debug_msg_logger(LOG_INFO, __FUNCTION__);1887 1888 1663 ch = input[0]; 1889 if (ch == '$') 1890 { 1664 if (ch == '$') { 1891 1665 /* Special case for "$$": PID */ 1892 sprintf ( tmp, "%d", (int) getpid ());1893 len = strlen 1666 sprintf(tmp, "%d",(int) getpid()); 1667 len = strlen(tmp); 1894 1668 if (len + *out_pos >= length) 1895 1669 goto expand_variable_out; 1896 1670 1897 memcpy 1671 memcpy(buffer + *out_pos, tmp, len + 1); 1898 1672 out_pos += len; 1899 return (input);1673 return input; 1900 1674 } 1901 1675 /* Ordinary variable expansion, possibly in braces */ 1902 if (ch != '{') 1903 { 1676 if (ch != '{') { 1904 1677 /* Simple variable expansion */ 1905 for (ptr = input; isalnum (ch) || (ch == '_') || (ch == ':');ch = *++ptr)1906 /* VOID */ 1678 for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr) 1679 /* VOID */; 1907 1680 len = ptr - input; 1908 1681 if ((size_t)len >= sizeof tmp) 1909 1682 goto expand_variable_out; 1910 1683 1911 safe_memcpy 1684 safe_memcpy(tmp, input, len); 1912 1685 input = ptr - 1; 1913 if ( ( env = get_variable_v2 (tmp, func, info) ) == NULL ) 1914 { 1915 msg_logger(LOG_INFO, bb_msg_variable_not_found, tmp); 1916 return (NULL); 1686 if ((env = get_variable_v2(tmp, func, info)) == NULL) { 1687 info_logger(LOG_INFO, bb_msg_variable_not_found, tmp); 1688 return NULL; 1917 1689 } 1918 len = strlen 1690 len = strlen(env); 1919 1691 if (len + *out_pos >= length) 1920 1692 goto expand_variable_out; 1921 1693 1922 memcpy 1694 memcpy(buffer + *out_pos, env, len + 1); 1923 1695 *out_pos += len; 1924 return (input);1696 return input; 1925 1697 } 1926 1698 /* Variable in braces: check for ':' tricks */ 1927 1699 ch = *++input; 1928 for (ptr = input; isalnum 1700 for (ptr = input; isalnum(ch) || (ch == '_'); ch = *++ptr) 1929 1701 /* VOID */; 1930 if (ch == '}') 1931 { 1702 if (ch == '}') { 1932 1703 /* Must be simple variable expansion with "${var}" */ 1933 1704 len = ptr - input; … … 1935 1706 goto expand_variable_out; 1936 1707 1937 safe_memcpy 1938 ptr = expand_variable (buffer, length, out_pos, tmp, func, info);1708 safe_memcpy(tmp, input, len); 1709 ptr = expand_variable(buffer, length, out_pos, tmp, func, info); 1939 1710 if (ptr == NULL) 1940 return (NULL); 1941 return (input + len); 1942 } 1943 if (ch != ':' || ptr[1] != '-' ) 1944 { 1945 msg_logger(LOG_INFO, "illegal char in var name"); 1946 return (NULL); 1711 return NULL; 1712 return input + len; 1713 } 1714 if (ch != ':' || ptr[1] != '-') { 1715 info_logger(LOG_INFO, "illegal char in var name"); 1716 return NULL; 1947 1717 } 1948 1718 /* It's that handy "${var:-word}" expression. Check if var is defined */ … … 1951 1721 goto expand_variable_out; 1952 1722 1953 safe_memcpy 1723 safe_memcpy(tmp, input, len); 1954 1724 /* Move input pointer to ':' */ 1955 1725 input = ptr; … … 1957 1727 ptr += 2; 1958 1728 ch = ptr[0]; 1959 for (open_braces = 1; open_braces > 0; ch = *++ptr) 1960 { 1961 switch (ch) 1962 { 1729 for (open_braces = 1; open_braces > 0; ch = *++ptr) { 1730 switch (ch) { 1963 1731 case '{': 1964 1732 ++open_braces; … … 1968 1736 break; 1969 1737 case '\0': 1970 msg_logger(LOG_INFO,"\"}\" not found in: %s", input);1971 return (NULL);1738 info_logger(LOG_INFO,"\"}\" not found in: %s", input); 1739 return NULL; 1972 1740 default: 1973 1741 break; … … 1976 1744 --ptr; 1977 1745 /* At this point ptr should point to closing brace of "${var:-word}" */ 1978 if ( ( env = get_variable_v2 (tmp, func, info) ) != NULL ) 1979 { 1746 if ((env = get_variable_v2(tmp, func, info)) != NULL) { 1980 1747 /* Found environment variable, so skip the input to the closing brace 1981 1748 and return the variable */ 1982 1749 input = ptr; 1983 len = strlen 1750 len = strlen(env); 1984 1751 if (len + *out_pos >= length) 1985 1752 goto expand_variable_out; 1986 1753 1987 memcpy 1754 memcpy(buffer + *out_pos, env, len + 1); 1988 1755 *out_pos += len; 1989 return (input);1756 return input; 1990 1757 } 1991 1758 /* Environment variable was not found, so process word. Advance input … … 1996 1763 goto expand_variable_out; 1997 1764 1998 safe_memcpy 1765 safe_memcpy(tmp, input, len); 1999 1766 input = ptr; 2000 if ( !st_expr_expand (tmp, STRING_LENGTH, tmp, func, info ))2001 return (NULL);2002 len = strlen 1767 if (!st_expr_expand(tmp, STRING_LENGTH, tmp, func, info)) 1768 return NULL; 1769 len = strlen(tmp); 2003 1770 if (len + *out_pos >= length) 2004 1771 goto expand_variable_out; 2005 1772 2006 memcpy 1773 memcpy(buffer + *out_pos, tmp, len + 1); 2007 1774 *out_pos += len; 2008 return (input);1775 return input; 2009 1776 expand_variable_out: 2010 msg_logger(LOG_INFO, bb_msg_small_buffer);2011 return (NULL);1777 info_logger(LOG_INFO, bb_msg_small_buffer); 1778 return NULL; 2012 1779 } /* End Function expand_variable */ 2013 1780 2014 1781 2015 static const char *get_variable_v2 2016 const char *(*func) 1782 static const char *get_variable_v2(const char *variable, 1783 const char *(*func)(const char *variable, void *info), 2017 1784 void *info) 2018 1785 /* [SUMMARY] Get a variable from the environment or . … … 2026 1793 const char *value; 2027 1794 2028 debug_msg_logger(LOG_INFO, __FUNCTION__); 2029 2030 if (func != NULL) 2031 { 2032 value = (*func) (variable, info); 1795 if (func != NULL) { 1796 value = (*func)(variable, info); 2033 1797 if (value != NULL) 2034 return (value);2035 } 2036 return getenv 1798 return value; 1799 } 1800 return getenv(variable); 2037 1801 } /* End Function get_variable */ 2038 1802
Note:
See TracChangeset
for help on using the changeset viewer.