Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/miscutils/devfsd.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/miscutils/devfsd.c
r1765 r2725 1 1 /* vi: set sw=4 ts=4: */ 2 2 /* 3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.3 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 4 4 */ 5 5 … … 54 54 Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia. 55 55 */ 56 57 //#include <sys/wait.h>58 //#include <sys/ioctl.h>59 //#include <sys/socket.h>60 #include <sys/un.h>61 #include <dirent.h>62 #include <syslog.h>63 #include <sys/sysmacros.h>64 56 #include "libbb.h" 65 57 #include "xregex.h" 66 58 #include <syslog.h> 59 60 #include <sys/un.h> 61 #include <sys/sysmacros.h> 67 62 68 63 /* Various defines taken from linux/major.h */ … … 81 76 /* Various defines taken from linux/devfs_fs.h */ 82 77 #define DEVFSD_PROTOCOL_REVISION_KERNEL 5 83 #define 78 #define DEVFSD_IOCTL_BASE 'd' 84 79 /* These are the various ioctls */ 85 80 #define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int) … … 98 93 /* Never change this otherwise the binary interface will change */ 99 94 100 struct devfsd_notify_struct 101 {/* Use native C types to ensure same types in kernel and user space */102 103 104 105 106 107 108 109 110 111 95 struct devfsd_notify_struct { 96 /* Use native C types to ensure same types in kernel and user space */ 97 unsigned int type; /* DEVFSD_NOTIFY_* value */ 98 unsigned int mode; /* Mode of the inode or device entry */ 99 unsigned int major; /* Major number of device entry */ 100 unsigned int minor; /* Minor number of device entry */ 101 unsigned int uid; /* Uid of process, inode or device entry */ 102 unsigned int gid; /* Gid of process, inode or device entry */ 103 unsigned int overrun_count; /* Number of lost events */ 104 unsigned int namelen; /* Number of characters not including '\0' */ 105 /* The device name MUST come last */ 106 char devname[DEVFS_PATHLEN]; /* This will be '\0' terminated */ 112 107 }; 113 108 … … 157 152 #define AC_RESTORE 11 158 153 159 struct permissions_type 160 { 154 struct permissions_type { 161 155 mode_t mode; 162 156 uid_t uid; … … 164 158 }; 165 159 166 struct execute_type 167 { 160 struct execute_type { 168 161 char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */ 169 162 }; 170 163 171 struct copy_type 172 { 164 struct copy_type { 173 165 const char *source; 174 166 const char *destination; 175 167 }; 176 168 177 struct action_type 178 { 169 struct action_type { 179 170 unsigned int what; 180 171 unsigned int when; 181 172 }; 182 173 183 struct config_entry_struct 184 { 174 struct config_entry_struct { 185 175 struct action_type action; 186 176 regex_t preg; … … 195 185 }; 196 186 197 struct get_variable_info 198 { 187 struct get_variable_info { 199 188 const struct devfsd_notify_struct *info; 200 189 const char *devname; … … 289 278 #define info_logger(p, fmt, args...) 290 279 #define msg_logger(p, fmt, args...) 291 #define msg_logger_and_die(p, fmt, args...) exit( 1)280 #define msg_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE) 292 281 #define error_logger(p, fmt, args...) 293 #define error_logger_and_die(p, fmt, args...) exit( 1)282 #define error_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE) 294 283 #endif 295 284 … … 337 326 /* Public functions follow */ 338 327 339 int devfsd_main(int argc, char **argv) ;328 int devfsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 340 329 int devfsd_main(int argc, char **argv) 341 330 { … … 372 361 373 362 fd = xopen(".devfsd", O_RDONLY); 374 375 if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) 376 bb_perror_msg_and_die("FD_CLOEXEC"); 377 363 close_on_exec_on(fd); 378 364 xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev); 379 365 … … 395 381 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0); 396 382 383 /* Set up SIGHUP and SIGUSR1 handlers */ 397 384 sigemptyset(&new_action.sa_mask); 398 385 new_action.sa_flags = 0; 399 400 /* Set up SIGHUP and SIGUSR1 handlers */401 386 new_action.sa_handler = signal_handler; 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);387 sigaction_set(SIGHUP, &new_action); 388 sigaction_set(SIGUSR1, &new_action); 389 390 printf("%s v%s started for %s\n", applet_name, DEVFSD_VERSION, mount_point); 406 391 407 392 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */ … … 412 397 413 398 if (ENABLE_DEVFSD_FG_NP && no_polling) 414 exit( 0);399 exit(EXIT_SUCCESS); 415 400 416 401 if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG) … … 469 454 return; 470 455 } 471 if ((fp = fopen(path, "r")) != NULL) { 456 fp = fopen_for_read(path); 457 if (fp != NULL) { 472 458 while (fgets(buf, STRING_LENGTH, fp) != NULL) { 473 459 /* Skip whitespace */ … … 564 550 new->action.what = AC_PERMISSIONS; 565 551 /* Get user and group */ 566 if ((ptr = strchr(p[0], '.')) == NULL) { 552 ptr = strchr(p[0], '.'); 553 if (ptr == NULL) { 567 554 msg = "UID.GID"; 568 555 goto process_config_line_err; /*"missing '.' in UID.GID"*/ … … 651 638 while (!caught_signal) { 652 639 errno = 0; 653 bytes = read(fd, (char *) &info, sizeof info);640 bytes = read(fd, (char *) &info, sizeof info); 654 641 if (caught_signal) 655 642 break; /* Must test for this first */ … … 749 736 750 737 static void action_modload(const struct devfsd_notify_struct *info, 751 const struct config_entry_struct *entry ATTRIBUTE_UNUSED)738 const struct config_entry_struct *entry UNUSED_PARAM) 752 739 /* [SUMMARY] Load a module. 753 740 <info> The devfs change. … … 765 752 argv[5] = NULL; 766 753 767 wait4pid(xspawn(argv));754 spawn_and_wait(argv); 768 755 free(argv[4]); 769 756 } /* End Function action_modload */ … … 797 784 } 798 785 argv[count] = NULL; 799 wait4pid(spawn(argv));786 spawn_and_wait(argv); 800 787 } /* End Function action_execute */ 801 788 … … 955 942 free(dpath); 956 943 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);944 copy_inode(dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX), spath, &source_stat); 958 945 959 946 if (S_ISDIR(source_stat.st_mode)) 960 dir_operation(RESTORE, spath, rootlen, NULL);947 dir_operation(RESTORE, spath, rootlen, NULL); 961 948 } 962 949 … … 983 970 /* Same type */ 984 971 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 972 source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1); 973 if ((source_len < 0) 974 || (dest_len = readlink(destpath, dest_link, STRING_LENGTH - 1)) < 0 987 975 ) 988 976 return FALSE; … … 1003 991 switch (source_stat->st_mode & S_IFMT) { 1004 992 case S_IFSOCK: 1005 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) 993 fd = socket(AF_UNIX, SOCK_STREAM, 0); 994 if (fd < 0) 1006 995 break; 1007 996 un_addr.sun_family = AF_UNIX; 1008 997 snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath); 1009 val = bind(fd, (struct sockaddr *) &un_addr,(int) sizeof un_addr);998 val = bind(fd, (struct sockaddr *) &un_addr, (int) sizeof un_addr); 1010 999 close(fd); 1011 1000 if (val != 0 || chmod(destpath, new_mode & ~S_IFMT) != 0) … … 1013 1002 goto do_chown; 1014 1003 case S_IFLNK: 1015 if ((val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1)) < 0) 1004 val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1); 1005 if (val < 0) 1016 1006 break; 1017 1007 symlink_val[val] = '\0'; … … 1020 1010 break; 1021 1011 case S_IFREG: 1022 if ((fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT)) < 0) 1012 fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT); 1013 if (fd < 0) 1023 1014 break; 1024 1015 close(fd); … … 1086 1077 return atoi(string); 1087 1078 1088 if (flag == UID && (pw_ent 1079 if (flag == UID && (pw_ent = getpwnam(string)) != NULL) 1089 1080 return pw_ent->pw_uid; 1090 1081 … … 1095 1086 1096 1087 if (ENABLE_DEVFSD_VERBOSE) 1097 msg_logger(LOG_ERR, "unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);1088 msg_logger(LOG_ERR, "unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]); 1098 1089 return 0; 1099 1090 }/* End Function get_uid_gid */ … … 1136 1127 { 1137 1128 static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */ 1138 1139 char hostname[STRING_LENGTH]; 1129 static char *hostname; 1130 1140 1131 struct get_variable_info *gv_info = info; 1141 1132 const char *field_names[] = { … … 1146 1137 int i; 1147 1138 1148 if (gethostname(hostname, STRING_LENGTH - 1) != 0) 1149 /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */ 1150 error_logger_and_die(LOG_ERR, "gethostname"); 1151 1152 hostname[STRING_LENGTH - 1] = '\0'; 1153 1139 if (!hostname) 1140 hostname = safe_gethostname(); 1154 1141 /* index_in_str_array returns i>=0 */ 1155 1142 i = index_in_str_array(field_names, variable); … … 1201 1188 char *path; 1202 1189 1203 if ((dp = warn_opendir(dir_name)) == NULL) 1190 dp = warn_opendir(dir_name); 1191 if (dp == NULL) 1204 1192 return; 1205 1193 … … 1343 1331 /* from compat_name.c */ 1344 1332 1345 struct translate_struct 1346 { 1333 struct translate_struct { 1347 1334 const char *match; /* The string to match to(up to length) */ 1348 1335 const char *format; /* Format of output, "%s" takes data past match string, … … 1446 1433 /* 2 ==scsi/disc, 4 == scsi/part */ 1447 1434 if (i == 2 || i == 4) 1448 compat_name = write_old_sd_name(buffer, major, minor, ((i == 2) ? "" : (ptr + 4)));1435 compat_name = write_old_sd_name(buffer, major, minor, ((i == 2) ? "" : (ptr + 4))); 1449 1436 1450 1437 /* 5 == scsi/mt */ … … 1585 1572 if (isspace(ch) ||(ch == '/') ||(ch == '\0')) { 1586 1573 /* User's own home directory: leave separator for next time */ 1587 if ((env = getenv("HOME")) == NULL) { 1574 env = getenv("HOME"); 1575 if (env == NULL) { 1588 1576 info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME"); 1589 1577 return FALSE; … … 1604 1592 safe_memcpy(tmp, input, len); 1605 1593 input = ptr - 1; 1606 if ((pwent = getpwnam(tmp)) == NULL) { 1594 pwent = getpwnam(tmp); 1595 if (pwent == NULL) { 1607 1596 info_logger(LOG_INFO, "no pwent for: %s", tmp); 1608 1597 return FALSE; … … 1664 1653 if (ch == '$') { 1665 1654 /* Special case for "$$": PID */ 1666 sprintf(tmp, "%d", (int) getpid());1655 sprintf(tmp, "%d", (int) getpid()); 1667 1656 len = strlen(tmp); 1668 1657 if (len + *out_pos >= length) … … 1684 1673 safe_memcpy(tmp, input, len); 1685 1674 input = ptr - 1; 1686 if ((env = get_variable_v2(tmp, func, info)) == NULL) { 1675 env = get_variable_v2(tmp, func, info); 1676 if (env == NULL) { 1687 1677 info_logger(LOG_INFO, bb_msg_variable_not_found, tmp); 1688 1678 return NULL; … … 1736 1726 break; 1737 1727 case '\0': 1738 info_logger(LOG_INFO, "\"}\" not found in: %s", input);1728 info_logger(LOG_INFO, "\"}\" not found in: %s", input); 1739 1729 return NULL; 1740 1730 default: … … 1744 1734 --ptr; 1745 1735 /* At this point ptr should point to closing brace of "${var:-word}" */ 1746 if ((env = get_variable_v2(tmp, func, info)) != NULL) { 1736 env = get_variable_v2(tmp, func, info); 1737 if (env != NULL) { 1747 1738 /* Found environment variable, so skip the input to the closing brace 1748 1739 and return the variable */
Note:
See TracChangeset
for help on using the changeset viewer.