[821] | 1 | /*
|
---|
| 2 | devfsd implementation for busybox
|
---|
| 3 |
|
---|
| 4 | Copyright (C) 2003 by Tito Ragusa <farmatito@tiscali.it>
|
---|
| 5 |
|
---|
| 6 | Busybox version is based on some previous work and ideas
|
---|
| 7 | Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
|
---|
| 8 |
|
---|
| 9 | devfsd.c
|
---|
| 10 |
|
---|
| 11 | Main file for devfsd (devfs daemon for Linux).
|
---|
| 12 |
|
---|
| 13 | Copyright (C) 1998-2002 Richard Gooch
|
---|
| 14 |
|
---|
| 15 | devfsd.h
|
---|
| 16 |
|
---|
| 17 | Header file for devfsd (devfs daemon for Linux).
|
---|
| 18 |
|
---|
| 19 | Copyright (C) 1998-2000 Richard Gooch
|
---|
| 20 |
|
---|
| 21 | compat_name.c
|
---|
| 22 |
|
---|
| 23 | Compatibility name file for devfsd (build compatibility names).
|
---|
| 24 |
|
---|
| 25 | Copyright (C) 1998-2002 Richard Gooch
|
---|
| 26 |
|
---|
| 27 | expression.c
|
---|
| 28 |
|
---|
| 29 | This code provides Borne Shell-like expression expansion.
|
---|
| 30 |
|
---|
| 31 | Copyright (C) 1997-1999 Richard Gooch
|
---|
| 32 |
|
---|
| 33 | This program is free software; you can redistribute it and/or modify
|
---|
| 34 | it under the terms of the GNU General Public License as published by
|
---|
| 35 | the Free Software Foundation; either version 2 of the License, or
|
---|
| 36 | (at your option) any later version.
|
---|
| 37 |
|
---|
| 38 | This program is distributed in the hope that it will be useful,
|
---|
| 39 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 40 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 41 | GNU General Public License for more details.
|
---|
| 42 |
|
---|
| 43 | You should have received a copy of the GNU General Public License
|
---|
| 44 | along with this program; if not, write to the Free Software
|
---|
| 45 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 46 |
|
---|
| 47 | Richard Gooch may be reached by email at rgooch@atnf.csiro.au
|
---|
| 48 | The postal address is:
|
---|
| 49 | Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
|
---|
| 50 | */
|
---|
| 51 |
|
---|
| 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>
|
---|
| 65 | #include <sys/un.h>
|
---|
| 66 | #include <dirent.h>
|
---|
| 67 | #include <fcntl.h>
|
---|
| 68 | #include <syslog.h>
|
---|
| 69 | #include <signal.h>
|
---|
| 70 | #include <errno.h>
|
---|
| 71 | #include <sys/sysmacros.h>
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | /* Various defines taken from linux/major.h */
|
---|
| 75 | #define IDE0_MAJOR 3
|
---|
| 76 | #define IDE1_MAJOR 22
|
---|
| 77 | #define IDE2_MAJOR 33
|
---|
| 78 | #define IDE3_MAJOR 34
|
---|
| 79 | #define IDE4_MAJOR 56
|
---|
| 80 | #define IDE5_MAJOR 57
|
---|
| 81 | #define IDE6_MAJOR 88
|
---|
| 82 | #define IDE7_MAJOR 89
|
---|
| 83 | #define IDE8_MAJOR 90
|
---|
| 84 | #define IDE9_MAJOR 91
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 | /* Various defines taken from linux/devfs_fs.h */
|
---|
| 88 | #define DEVFSD_PROTOCOL_REVISION_KERNEL 5
|
---|
| 89 | #define DEVFSD_IOCTL_BASE 'd'
|
---|
| 90 | /* These are the various ioctls */
|
---|
| 91 | #define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int)
|
---|
| 92 | #define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int)
|
---|
| 93 | #define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int)
|
---|
| 94 | #define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
|
---|
| 95 | #define DEVFSD_NOTIFY_REGISTERED 0
|
---|
| 96 | #define DEVFSD_NOTIFY_UNREGISTERED 1
|
---|
| 97 | #define DEVFSD_NOTIFY_ASYNC_OPEN 2
|
---|
| 98 | #define DEVFSD_NOTIFY_CLOSE 3
|
---|
| 99 | #define DEVFSD_NOTIFY_LOOKUP 4
|
---|
| 100 | #define DEVFSD_NOTIFY_CHANGE 5
|
---|
| 101 | #define DEVFSD_NOTIFY_CREATE 6
|
---|
| 102 | #define DEVFSD_NOTIFY_DELETE 7
|
---|
| 103 | #define DEVFS_PATHLEN 1024
|
---|
| 104 | /* Never change this otherwise the binary interface will change */
|
---|
| 105 |
|
---|
| 106 | struct devfsd_notify_struct
|
---|
| 107 | { /* Use native C types to ensure same types in kernel and user space */
|
---|
| 108 | unsigned int type; /* DEVFSD_NOTIFY_* value */
|
---|
| 109 | unsigned int mode; /* Mode of the inode or device entry */
|
---|
| 110 | unsigned int major; /* Major number of device entry */
|
---|
| 111 | unsigned int minor; /* Minor number of device entry */
|
---|
| 112 | unsigned int uid; /* Uid of process, inode or device entry */
|
---|
| 113 | unsigned int gid; /* Gid of process, inode or device entry */
|
---|
| 114 | unsigned int overrun_count; /* Number of lost events */
|
---|
| 115 | unsigned int namelen; /* Number of characters not including '\0' */
|
---|
| 116 | /* The device name MUST come last */
|
---|
| 117 | char devname[DEVFS_PATHLEN]; /* This will be '\0' terminated */
|
---|
| 118 | };
|
---|
| 119 |
|
---|
| 120 | #define BUFFER_SIZE 16384
|
---|
| 121 | #define DEVFSD_VERSION "1.3.25"
|
---|
| 122 | #define CONFIG_FILE "/etc/devfsd.conf"
|
---|
| 123 | #define MODPROBE "/sbin/modprobe"
|
---|
| 124 | #define MODPROBE_SWITCH_1 "-k"
|
---|
| 125 | #define MODPROBE_SWITCH_2 "-C"
|
---|
| 126 | #define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
|
---|
| 127 | #define MAX_ARGS (6 + 1)
|
---|
| 128 | #define MAX_SUBEXPR 10
|
---|
| 129 | #define STRING_LENGTH 255
|
---|
| 130 |
|
---|
| 131 | /* for get_uid_gid() */
|
---|
| 132 | #define UID 0
|
---|
| 133 | #define GID 1
|
---|
| 134 |
|
---|
| 135 | /* fork_and_execute() */
|
---|
| 136 | # define DIE 1
|
---|
| 137 | # define NO_DIE 0
|
---|
| 138 |
|
---|
| 139 | /* for dir_operation() */
|
---|
| 140 | #define RESTORE 0
|
---|
| 141 | #define SERVICE 1
|
---|
| 142 | #define READ_CONFIG 2
|
---|
| 143 |
|
---|
| 144 | /* Update only after changing code to reflect new protocol */
|
---|
| 145 | #define DEVFSD_PROTOCOL_REVISION_DAEMON 5
|
---|
| 146 |
|
---|
| 147 | /* Compile-time check */
|
---|
| 148 | #if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
|
---|
| 149 | #error protocol version mismatch. Update your kernel headers
|
---|
| 150 | #endif
|
---|
| 151 |
|
---|
| 152 | #define AC_PERMISSIONS 0
|
---|
| 153 | #define AC_MODLOAD 1
|
---|
| 154 | #define AC_EXECUTE 2
|
---|
| 155 | #define AC_MFUNCTION 3 /* not supported by busybox */
|
---|
| 156 | #define AC_CFUNCTION 4 /* not supported by busybox */
|
---|
| 157 | #define AC_COPY 5
|
---|
| 158 | #define AC_IGNORE 6
|
---|
| 159 | #define AC_MKOLDCOMPAT 7
|
---|
| 160 | #define AC_MKNEWCOMPAT 8
|
---|
| 161 | #define AC_RMOLDCOMPAT 9
|
---|
| 162 | #define AC_RMNEWCOMPAT 10
|
---|
| 163 | #define AC_RESTORE 11
|
---|
| 164 |
|
---|
| 165 | struct permissions_type
|
---|
| 166 | {
|
---|
| 167 | mode_t mode;
|
---|
| 168 | uid_t uid;
|
---|
| 169 | gid_t gid;
|
---|
| 170 | };
|
---|
| 171 |
|
---|
| 172 | struct execute_type
|
---|
| 173 | {
|
---|
| 174 | char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */
|
---|
| 175 | };
|
---|
| 176 |
|
---|
| 177 | struct copy_type
|
---|
| 178 | {
|
---|
| 179 | const char *source;
|
---|
| 180 | const char *destination;
|
---|
| 181 | };
|
---|
| 182 |
|
---|
| 183 | struct action_type
|
---|
| 184 | {
|
---|
| 185 | unsigned int what;
|
---|
| 186 | unsigned int when;
|
---|
| 187 | };
|
---|
| 188 |
|
---|
| 189 | struct config_entry_struct
|
---|
| 190 | {
|
---|
| 191 | struct action_type action;
|
---|
| 192 | regex_t preg;
|
---|
| 193 | union
|
---|
| 194 | {
|
---|
| 195 | struct permissions_type permissions;
|
---|
| 196 | struct execute_type execute;
|
---|
| 197 | struct copy_type copy;
|
---|
| 198 | }
|
---|
| 199 | u;
|
---|
| 200 | struct config_entry_struct *next;
|
---|
| 201 | };
|
---|
| 202 |
|
---|
| 203 | struct get_variable_info
|
---|
| 204 | {
|
---|
| 205 | const struct devfsd_notify_struct *info;
|
---|
| 206 | const char *devname;
|
---|
| 207 | char devpath[STRING_LENGTH];
|
---|
| 208 | };
|
---|
| 209 |
|
---|
| 210 | static void dir_operation(int , const char * , int, unsigned long* );
|
---|
| 211 | static void service(struct stat statbuf, char *path);
|
---|
| 212 | static int st_expr_expand(char *, unsigned, const char *, const char *(*) (const char *, void *), void *);
|
---|
| 213 | static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
|
---|
| 214 | static int mksymlink (const char *oldpath, const char *newpath);
|
---|
| 215 | static void read_config_file (char *path, int optional, unsigned long *event_mask);
|
---|
| 216 | static void process_config_line (const char *, unsigned long *);
|
---|
| 217 | static int do_servicing (int, unsigned long);
|
---|
| 218 | static void service_name (const struct devfsd_notify_struct *);
|
---|
| 219 | static void action_permissions (const struct devfsd_notify_struct *, const struct config_entry_struct *);
|
---|
| 220 | static void action_execute (const struct devfsd_notify_struct *, const struct config_entry_struct *,
|
---|
| 221 | const regmatch_t *, unsigned);
|
---|
| 222 | static void action_modload (const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
|
---|
| 223 | static void action_copy (const struct devfsd_notify_struct *, const struct config_entry_struct *,
|
---|
| 224 | const regmatch_t *, unsigned);
|
---|
| 225 | static void action_compat (const struct devfsd_notify_struct *, unsigned);
|
---|
| 226 | static void free_config (void);
|
---|
| 227 | static void restore(char *spath, struct stat source_stat, int rootlen);
|
---|
| 228 | static int copy_inode (const char *, const struct stat *, mode_t, const char *, const struct stat *);
|
---|
| 229 | static mode_t get_mode (const char *);
|
---|
| 230 | static void signal_handler (int);
|
---|
| 231 | static const char *get_variable (const char *, void *);
|
---|
| 232 | static int make_dir_tree (const char *);
|
---|
| 233 | 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 );
|
---|
| 236 | 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 *(*) (const char *, void *), void *);
|
---|
| 239 | static char get_old_ide_name (unsigned , unsigned);
|
---|
| 240 | static char *write_old_sd_name (char *, unsigned, unsigned, char *);
|
---|
| 241 |
|
---|
| 242 | /* 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);
|
---|
| 251 |
|
---|
| 252 | /* Structs and vars */
|
---|
| 253 | static struct config_entry_struct *first_config = NULL;
|
---|
| 254 | static struct config_entry_struct *last_config = NULL;
|
---|
| 255 | static const char *mount_point = NULL;
|
---|
| 256 | static volatile int caught_signal = FALSE;
|
---|
| 257 | 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},
|
---|
| 269 | };
|
---|
| 270 |
|
---|
| 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}
|
---|
| 286 | };
|
---|
| 287 |
|
---|
| 288 | /* Busybox messages */
|
---|
| 289 |
|
---|
| 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 | }
|
---|
| 325 |
|
---|
| 326 | /* 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)
|
---|
| 333 | #else
|
---|
| 334 | #define debug_msg_logger(x, fmt, args...)
|
---|
| 335 | #endif
|
---|
| 336 | #else
|
---|
| 337 | #define debug_msg_logger(x, fmt, args...)
|
---|
| 338 | #define msg_logger(p, fmt, args...)
|
---|
| 339 | #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...)
|
---|
| 343 | #endif
|
---|
| 344 |
|
---|
| 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 );
|
---|
| 384 | dest[len] = '\0';
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 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;
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 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' )
|
---|
| 408 | return 1;
|
---|
| 409 | return scan_dev_name_common(d, n, 0, ptr);
|
---|
| 410 | }
|
---|
| 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 | {
|
---|
| 414 | 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 | {
|
---|
| 418 | return 10;
|
---|
| 419 | }
|
---|
| 420 | else if(d[0]=='v' && d[1]=='c' && d[2]=='c' && d[3]=='/')
|
---|
| 421 | {
|
---|
| 422 | return 11;
|
---|
| 423 | }
|
---|
| 424 | else if(d[0]=='p' && d[1]=='t' && d[2]=='y' && d[3]=='/')
|
---|
| 425 | {
|
---|
| 426 | return 12;
|
---|
| 427 | }
|
---|
| 428 | return 0;
|
---|
| 429 | }
|
---|
| 430 |
|
---|
| 431 | /* Public functions follow */
|
---|
| 432 |
|
---|
| 433 | int devfsd_main (int argc, char **argv)
|
---|
| 434 | {
|
---|
| 435 | int print_version = FALSE;
|
---|
| 436 | int do_daemon = TRUE;
|
---|
| 437 | int no_polling = FALSE;
|
---|
| 438 | int do_scan;
|
---|
| 439 | int fd, proto_rev, count;
|
---|
| 440 | unsigned long event_mask = 0;
|
---|
| 441 | struct sigaction new_action;
|
---|
| 442 | struct initial_symlink_struct *curr;
|
---|
| 443 |
|
---|
| 444 | if (argc < 2)
|
---|
| 445 | bb_show_usage();
|
---|
| 446 |
|
---|
| 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;
|
---|
| 459 | else
|
---|
| 460 | bb_show_usage();
|
---|
| 461 | }
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 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");
|
---|
| 480 |
|
---|
| 481 | /*setup initial entries */
|
---|
| 482 | for (curr = initial_symlinks; curr->dest != NULL; ++curr)
|
---|
| 483 | symlink (curr->dest, curr->name);
|
---|
| 484 |
|
---|
| 485 | /* NB: The check for CONFIG_FILE is done in read_config_file() */
|
---|
| 486 |
|
---|
| 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);
|
---|
| 492 | if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
|
---|
| 493 | bb_error_msg_and_die( "%s mismatch!",bb_msg_proto_rev);
|
---|
| 494 | exit(EXIT_SUCCESS); /* -v */
|
---|
| 495 | }
|
---|
| 496 | /* Tell kernel we are special (i.e. we get to see hidden entries) */
|
---|
| 497 | do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
|
---|
| 498 |
|
---|
| 499 | sigemptyset (&new_action.sa_mask);
|
---|
| 500 | new_action.sa_flags = 0;
|
---|
| 501 |
|
---|
| 502 | /* Set up SIGHUP and SIGUSR1 handlers */
|
---|
| 503 | 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);
|
---|
| 508 |
|
---|
| 509 | /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
|
---|
| 510 | umask (0);
|
---|
| 511 | read_config_file (CONFIG_FILE, FALSE, &event_mask);
|
---|
| 512 | /* Do the scan before forking, so that boot scripts see the finished product */
|
---|
| 513 | dir_operation(SERVICE,mount_point,0,NULL);
|
---|
| 514 |
|
---|
| 515 | if (ENABLE_DEVFSD_FG_NP && no_polling)
|
---|
| 516 | exit (0);
|
---|
| 517 | if (do_daemon)
|
---|
| 518 | {
|
---|
| 519 | /* 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);
|
---|
| 533 | if (do_scan)
|
---|
| 534 | dir_operation(SERVICE,mount_point,0,NULL);
|
---|
| 535 | }
|
---|
| 536 | } /* End Function main */
|
---|
| 537 |
|
---|
| 538 |
|
---|
| 539 | /* Private functions follow */
|
---|
| 540 |
|
---|
| 541 | static void read_config_file (char *path, int optional, unsigned long *event_mask)
|
---|
| 542 | /* [SUMMARY] Read a configuration database.
|
---|
| 543 | <path> The path to read the database from. If this is a directory, all
|
---|
| 544 | entries in that directory will be read (except hidden entries).
|
---|
| 545 | <optional> If TRUE, the routine will silently ignore a missing config file.
|
---|
| 546 | <event_mask> The event mask is written here. This is not initialised.
|
---|
| 547 | [RETURNS] Nothing.
|
---|
| 548 | */
|
---|
| 549 | {
|
---|
| 550 | struct stat statbuf;
|
---|
| 551 | FILE *fp;
|
---|
| 552 | 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 | {
|
---|
| 559 | /* Don't read 0 length files: ignored */
|
---|
| 560 | /*if( statbuf.st_size == 0 )
|
---|
| 561 | 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);
|
---|
| 569 | return;
|
---|
| 570 | }
|
---|
| 571 | if ( ( fp = fopen (path, "r") ) != NULL )
|
---|
| 572 | {
|
---|
| 573 | while (fgets (buf, STRING_LENGTH, fp) != NULL)
|
---|
| 574 | {
|
---|
| 575 | /* Skip whitespace */
|
---|
| 576 | for (line = buf; isspace (*line); ++line)
|
---|
| 577 | /*VOID*/;
|
---|
| 578 | if (line[0] == '\0' || line[0] == '#' )
|
---|
| 579 | continue;
|
---|
| 580 | process_config_line (line, event_mask);
|
---|
| 581 | }
|
---|
| 582 | fclose (fp);
|
---|
| 583 | } else {
|
---|
| 584 | goto read_config_file_err;
|
---|
| 585 | }
|
---|
| 586 | } else {
|
---|
| 587 | 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;
|
---|
| 592 | } /* End Function read_config_file */
|
---|
| 593 |
|
---|
| 594 | static void process_config_line (const char *line, unsigned long *event_mask)
|
---|
| 595 | /* [SUMMARY] Process a line from a configuration file.
|
---|
| 596 | <line> The configuration line.
|
---|
| 597 | <event_mask> The event mask is written here. This is not initialised.
|
---|
| 598 | [RETURNS] Nothing.
|
---|
| 599 | */
|
---|
| 600 | {
|
---|
| 601 | int num_args, count;
|
---|
| 602 | struct config_entry_struct *new;
|
---|
| 603 | char p[MAX_ARGS][STRING_LENGTH];
|
---|
| 604 | char when[STRING_LENGTH], what[STRING_LENGTH];
|
---|
| 605 | char name[STRING_LENGTH];
|
---|
| 606 | char * msg="";
|
---|
| 607 | char *ptr;
|
---|
| 608 | int i;
|
---|
| 609 |
|
---|
| 610 | /* !!!! 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",
|
---|
| 622 | when, name, what,
|
---|
| 623 | p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
|
---|
| 624 |
|
---|
| 625 | i = compare_string_array(options, when );
|
---|
| 626 |
|
---|
| 627 | /*"CLEAR_CONFIG"*/
|
---|
| 628 | if( i == 0)
|
---|
| 629 | {
|
---|
| 630 | free_config ();
|
---|
| 631 | *event_mask = 0;
|
---|
| 632 | return;
|
---|
| 633 | }
|
---|
| 634 |
|
---|
| 635 | if ( num_args < 2)
|
---|
| 636 | goto process_config_line_err;
|
---|
| 637 |
|
---|
| 638 | /* "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);
|
---|
| 644 | return;
|
---|
| 645 | }
|
---|
| 646 | /* "RESTORE" */
|
---|
| 647 | if( i == 3)
|
---|
| 648 | {
|
---|
| 649 | dir_operation(RESTORE,name, strlen (name),NULL);
|
---|
| 650 | return;
|
---|
| 651 | }
|
---|
| 652 | if (num_args < 3)
|
---|
| 653 | goto process_config_line_err;
|
---|
| 654 |
|
---|
| 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)
|
---|
| 661 | continue;
|
---|
| 662 | new->action.when = event_types[count].type;
|
---|
| 663 | break;
|
---|
| 664 | }
|
---|
| 665 | if (event_types[count].config_name == NULL)
|
---|
| 666 | {
|
---|
| 667 | msg="WHEN in";
|
---|
| 668 | goto process_config_line_err;
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | i = compare_string_array(options, what );
|
---|
| 672 |
|
---|
| 673 | switch(i)
|
---|
| 674 | {
|
---|
| 675 | case 4: /* "PERMISSIONS" */
|
---|
| 676 | new->action.what = AC_PERMISSIONS;
|
---|
| 677 | /* Get user and group */
|
---|
| 678 | if ( ( ptr = strchr (p[0], '.') ) == NULL )
|
---|
| 679 | {
|
---|
| 680 | msg="UID.GID";
|
---|
| 681 | goto process_config_line_err; /*"missing '.' in UID.GID"*/
|
---|
| 682 | }
|
---|
| 683 |
|
---|
| 684 | *ptr++ = '\0';
|
---|
| 685 | new->u.permissions.uid = get_uid_gid (UID, p[0]);
|
---|
| 686 | new->u.permissions.gid = get_uid_gid (GID, ptr);
|
---|
| 687 | /* Get mode */
|
---|
| 688 | new->u.permissions.mode = get_mode (p[1]);
|
---|
| 689 | break;
|
---|
| 690 | case 5: /* MODLOAD */
|
---|
| 691 | /*This action will pass "/dev/$devname" (i.e. "/dev/" prefixed to
|
---|
| 692 | the device name) to the module loading facility. In addition,
|
---|
| 693 | the /etc/modules.devfs configuration file is used.*/
|
---|
| 694 | if (ENABLE_DEVFSD_MODLOAD)
|
---|
| 695 | new->action.what = AC_MODLOAD;
|
---|
| 696 | break;
|
---|
| 697 | case 6: /* EXECUTE */
|
---|
| 698 | new->action.what = AC_EXECUTE;
|
---|
| 699 | num_args -= 3;
|
---|
| 700 |
|
---|
| 701 | for (count = 0; count < num_args; ++count)
|
---|
| 702 | new->u.execute.argv[count] = bb_xstrdup (p[count]);
|
---|
| 703 |
|
---|
| 704 | new->u.execute.argv[num_args] = NULL;
|
---|
| 705 | break;
|
---|
| 706 | case 7: /* COPY */
|
---|
| 707 | new->action.what = AC_COPY;
|
---|
| 708 | num_args -= 3;
|
---|
| 709 | if (num_args != 2)
|
---|
| 710 | goto process_config_line_err; /* missing path and function in line */
|
---|
| 711 |
|
---|
| 712 | new->u.copy.source = bb_xstrdup (p[0]);
|
---|
| 713 | new->u.copy.destination = bb_xstrdup (p[1]);
|
---|
| 714 | break;
|
---|
| 715 | case 8: /* IGNORE */
|
---|
| 716 | /* FALLTROUGH */
|
---|
| 717 | case 9: /* MKOLDCOMPAT */
|
---|
| 718 | /* FALLTROUGH */
|
---|
| 719 | case 10: /* MKNEWCOMPAT */
|
---|
| 720 | /* FALLTROUGH */
|
---|
| 721 | case 11:/* RMOLDCOMPAT */
|
---|
| 722 | /* FALLTROUGH */
|
---|
| 723 | case 12: /* RMNEWCOMPAT */
|
---|
| 724 | /* AC_IGNORE 6
|
---|
| 725 | AC_MKOLDCOMPAT 7
|
---|
| 726 | AC_MKNEWCOMPAT 8
|
---|
| 727 | AC_RMOLDCOMPAT 9
|
---|
| 728 | AC_RMNEWCOMPAT 10*/
|
---|
| 729 | new->action.what = i - 2;
|
---|
| 730 | break;
|
---|
| 731 | default:
|
---|
| 732 | msg ="WHAT in";
|
---|
| 733 | goto process_config_line_err;
|
---|
| 734 | /*esac*/
|
---|
| 735 | } /* switch (i) */
|
---|
| 736 |
|
---|
| 737 | xregcomp( &new->preg, name, REG_EXTENDED);
|
---|
| 738 |
|
---|
| 739 | *event_mask |= 1 << new->action.when;
|
---|
| 740 | new->next = NULL;
|
---|
| 741 | if (first_config == NULL)
|
---|
| 742 | first_config = new;
|
---|
| 743 | else
|
---|
| 744 | last_config->next = new;
|
---|
| 745 | last_config = new;
|
---|
| 746 | return;
|
---|
| 747 | process_config_line_err:
|
---|
| 748 | msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line);
|
---|
| 749 | } /* End Function process_config_line */
|
---|
| 750 |
|
---|
| 751 | static int do_servicing (int fd, unsigned long event_mask)
|
---|
| 752 | /* [SUMMARY] Service devfs changes until a signal is received.
|
---|
| 753 | <fd> The open control file.
|
---|
| 754 | <event_mask> The event mask.
|
---|
| 755 | [RETURNS] TRUE if SIGHUP was caught, else FALSE.
|
---|
| 756 | */
|
---|
| 757 | {
|
---|
| 758 | ssize_t bytes;
|
---|
| 759 | 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 | {
|
---|
| 769 | errno = 0;
|
---|
| 770 | bytes = read (fd, (char *) &info, sizeof info);
|
---|
| 771 | if (caught_signal)
|
---|
| 772 | break; /* Must test for this first */
|
---|
| 773 | if (errno == EINTR)
|
---|
| 774 | continue; /* Yes, the order is important */
|
---|
| 775 | if (bytes < 1)
|
---|
| 776 | break;
|
---|
| 777 | service_name (&info);
|
---|
| 778 | }
|
---|
| 779 | if (caught_signal)
|
---|
| 780 | {
|
---|
| 781 | int c_sighup = caught_sighup;
|
---|
| 782 |
|
---|
| 783 | caught_signal = FALSE;
|
---|
| 784 | caught_sighup = FALSE;
|
---|
| 785 | return (c_sighup);
|
---|
| 786 | }
|
---|
| 787 | msg_logger_and_die(LOG_ERR, "read error on control file");
|
---|
| 788 | } /* End Function do_servicing */
|
---|
| 789 |
|
---|
| 790 | static void service_name (const struct devfsd_notify_struct *info)
|
---|
| 791 | /* [SUMMARY] Service a single devfs change.
|
---|
| 792 | <info> The devfs change.
|
---|
| 793 | [RETURNS] Nothing.
|
---|
| 794 | */
|
---|
| 795 | {
|
---|
| 796 | unsigned int n;
|
---|
| 797 | regmatch_t mbuf[MAX_SUBEXPR];
|
---|
| 798 | struct config_entry_struct *entry;
|
---|
| 799 |
|
---|
| 800 | debug_msg_logger(LOG_INFO, __FUNCTION__);
|
---|
| 801 | if (ENABLE_DEBUG && info->overrun_count > 0)
|
---|
| 802 | debug_msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
|
---|
| 803 |
|
---|
| 804 | /* 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 | {
|
---|
| 815 | /* 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 )
|
---|
| 817 | continue;
|
---|
| 818 | for (n = 0; (n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
|
---|
| 819 | /* VOID */;
|
---|
| 820 |
|
---|
| 821 | debug_msg_logger(LOG_INFO, "%s: action.what %d", __FUNCTION__, entry->action.what);
|
---|
| 822 |
|
---|
| 823 | switch (entry->action.what)
|
---|
| 824 | {
|
---|
| 825 | case AC_PERMISSIONS:
|
---|
| 826 | action_permissions (info, entry);
|
---|
| 827 | break;
|
---|
| 828 | case AC_MODLOAD:
|
---|
| 829 | if(ENABLE_DEVFSD_MODLOAD)
|
---|
| 830 | action_modload (info, entry);
|
---|
| 831 | break;
|
---|
| 832 | case AC_EXECUTE:
|
---|
| 833 | action_execute (info, entry, mbuf, n);
|
---|
| 834 | break;
|
---|
| 835 | case AC_COPY:
|
---|
| 836 | action_copy (info, entry, mbuf, n);
|
---|
| 837 | break;
|
---|
| 838 | case AC_IGNORE:
|
---|
| 839 | return;
|
---|
| 840 | /*break;*/
|
---|
| 841 | case AC_MKOLDCOMPAT:
|
---|
| 842 | case AC_MKNEWCOMPAT:
|
---|
| 843 | case AC_RMOLDCOMPAT:
|
---|
| 844 | case AC_RMNEWCOMPAT:
|
---|
| 845 | action_compat (info, entry->action.what);
|
---|
| 846 | break;
|
---|
| 847 | default:
|
---|
| 848 | msg_logger_and_die(LOG_ERR, "Unknown action");
|
---|
| 849 | }
|
---|
| 850 | }
|
---|
| 851 | } /* End Function service_name */
|
---|
| 852 |
|
---|
| 853 | static void action_permissions (const struct devfsd_notify_struct *info,
|
---|
| 854 | const struct config_entry_struct *entry)
|
---|
| 855 | /* [SUMMARY] Update permissions for a device entry.
|
---|
| 856 | <info> The devfs change.
|
---|
| 857 | <entry> The config file entry.
|
---|
| 858 | [RETURNS] Nothing.
|
---|
| 859 | */
|
---|
| 860 | {
|
---|
| 861 | struct stat statbuf;
|
---|
| 862 |
|
---|
| 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 | }
|
---|
| 871 | } /* End Function action_permissions */
|
---|
| 872 |
|
---|
| 873 | static void action_modload (const struct devfsd_notify_struct *info,
|
---|
| 874 | const struct config_entry_struct *entry ATTRIBUTE_UNUSED)
|
---|
| 875 | /* [SUMMARY] Load a module.
|
---|
| 876 | <info> The devfs change.
|
---|
| 877 | <entry> The config file entry.
|
---|
| 878 | [RETURNS] Nothing.
|
---|
| 879 | */
|
---|
| 880 | {
|
---|
| 881 | 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;
|
---|
| 889 | argv[5] = NULL;
|
---|
| 890 |
|
---|
| 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);
|
---|
| 894 | } /* End Function action_modload */
|
---|
| 895 |
|
---|
| 896 | static void action_execute (const struct devfsd_notify_struct *info,
|
---|
| 897 | const struct config_entry_struct *entry,
|
---|
| 898 | const regmatch_t *regexpr, unsigned int numexpr)
|
---|
| 899 | /* [SUMMARY] Execute a programme.
|
---|
| 900 | <info> The devfs change.
|
---|
| 901 | <entry> The config file entry.
|
---|
| 902 | <regexpr> The number of subexpression (start, end) offsets within the
|
---|
| 903 | device name.
|
---|
| 904 | <numexpr> The number of elements within <<regexpr>>.
|
---|
| 905 | [RETURNS] Nothing.
|
---|
| 906 | */
|
---|
| 907 | {
|
---|
| 908 | unsigned int count;
|
---|
| 909 | struct get_variable_info gv_info;
|
---|
| 910 | char *argv[MAX_ARGS + 1];
|
---|
| 911 | char largv[MAX_ARGS + 1][STRING_LENGTH];
|
---|
| 912 |
|
---|
| 913 | debug_msg_logger(LOG_INFO ,__FUNCTION__);
|
---|
| 914 | gv_info.info = info;
|
---|
| 915 | 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,
|
---|
| 920 | entry->u.execute.argv[count],
|
---|
| 921 | get_variable, &gv_info,
|
---|
| 922 | gv_info.devname, regexpr, numexpr );
|
---|
| 923 | argv[count] = largv[count];
|
---|
| 924 | }
|
---|
| 925 | argv[count] = NULL;
|
---|
| 926 | fork_and_execute(NO_DIE, argv[0], argv);
|
---|
| 927 | } /* End Function action_execute */
|
---|
| 928 |
|
---|
| 929 |
|
---|
| 930 | static void action_copy (const struct devfsd_notify_struct *info,
|
---|
| 931 | const struct config_entry_struct *entry,
|
---|
| 932 | const regmatch_t *regexpr, unsigned int numexpr)
|
---|
| 933 | /* [SUMMARY] Copy permissions.
|
---|
| 934 | <info> The devfs change.
|
---|
| 935 | <entry> The config file entry.
|
---|
| 936 | <regexpr> This list of subexpression (start, end) offsets within the
|
---|
| 937 | device name.
|
---|
| 938 | <numexpr> The number of elements in <<regexpr>>.
|
---|
| 939 | [RETURNS] Nothing.
|
---|
| 940 | */
|
---|
| 941 | {
|
---|
| 942 | mode_t new_mode;
|
---|
| 943 | struct get_variable_info gv_info;
|
---|
| 944 | struct stat source_stat, dest_stat;
|
---|
| 945 | char source[STRING_LENGTH], destination[STRING_LENGTH];
|
---|
| 946 | int ret = 0;
|
---|
| 947 |
|
---|
| 948 | debug_msg_logger(LOG_INFO, __FUNCTION__);
|
---|
| 949 |
|
---|
| 950 | dest_stat.st_mode = 0;
|
---|
| 951 |
|
---|
| 952 | if ( (info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK (info->mode) )
|
---|
| 953 | return;
|
---|
| 954 | gv_info.info = info;
|
---|
| 955 | gv_info.devname = info->devname;
|
---|
| 956 |
|
---|
| 957 | snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname);
|
---|
| 958 | expand_expression (source, STRING_LENGTH, entry->u.copy.source,
|
---|
| 959 | get_variable, &gv_info, gv_info.devname,
|
---|
| 960 | regexpr, numexpr);
|
---|
| 961 |
|
---|
| 962 | expand_expression (destination, STRING_LENGTH, entry->u.copy.destination,
|
---|
| 963 | get_variable, &gv_info, gv_info.devname,
|
---|
| 964 | regexpr, numexpr);
|
---|
| 965 |
|
---|
| 966 | if ( !make_dir_tree (destination) || lstat (source, &source_stat) != 0)
|
---|
| 967 | return;
|
---|
| 968 | lstat (destination, &dest_stat);
|
---|
| 969 | new_mode = source_stat.st_mode & ~S_ISVTX;
|
---|
| 970 | if (info->type == DEVFSD_NOTIFY_CREATE)
|
---|
| 971 | new_mode |= S_ISVTX;
|
---|
| 972 | else if ( (info->type == DEVFSD_NOTIFY_CHANGE) && (dest_stat.st_mode & S_ISVTX) )
|
---|
| 973 | new_mode |= S_ISVTX;
|
---|
| 974 | ret = copy_inode (destination, &dest_stat, new_mode, source, &source_stat);
|
---|
| 975 | if (ENABLE_DEBUG && ret && (errno != EEXIST))
|
---|
| 976 | debug_msg_logger(LOG_ERR, "copy_inode: %s to %s: %m", source, destination);
|
---|
| 977 | return;
|
---|
| 978 | } /* End Function action_copy */
|
---|
| 979 |
|
---|
| 980 | static void action_compat (const struct devfsd_notify_struct *info, unsigned int action)
|
---|
| 981 | /* [SUMMARY] Process a compatibility request.
|
---|
| 982 | <info> The devfs change.
|
---|
| 983 | <action> The action to take.
|
---|
| 984 | [RETURNS] Nothing.
|
---|
| 985 | */
|
---|
| 986 | {
|
---|
| 987 | int ret;
|
---|
| 988 | const char *compat_name = NULL;
|
---|
| 989 | const char *dest_name = info->devname;
|
---|
| 990 | char *ptr=NULL;
|
---|
| 991 | char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
|
---|
| 992 | int mode, host, bus, target, lun;
|
---|
| 993 | unsigned int i;
|
---|
| 994 | char rewind_;
|
---|
| 995 | /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
|
---|
| 996 | static const char *const fmt[] = {
|
---|
| 997 | NULL ,
|
---|
| 998 | "sg/c%db%dt%du%d", /* scsi/generic */
|
---|
| 999 | "sd/c%db%dt%du%d", /* scsi/disc */
|
---|
| 1000 | "sr/c%db%dt%du%d", /* scsi/cd */
|
---|
| 1001 | "sd/c%db%dt%du%dp%d", /* scsi/part */
|
---|
| 1002 | "st/c%db%dt%du%dm%d%c", /* scsi/mt */
|
---|
| 1003 | "ide/hd/c%db%dt%du%d", /* ide/host/disc */
|
---|
| 1004 | "ide/cd/c%db%dt%du%d", /* ide/host/cd */
|
---|
| 1005 | "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
|
---|
| 1006 | "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
|
---|
| 1007 | NULL
|
---|
| 1008 | };
|
---|
| 1009 |
|
---|
| 1010 | /* First construct compatibility name */
|
---|
| 1011 | switch (action)
|
---|
| 1012 | {
|
---|
| 1013 | case AC_MKOLDCOMPAT:
|
---|
| 1014 | case AC_RMOLDCOMPAT:
|
---|
| 1015 | compat_name = get_old_name (info->devname, info->namelen, compat_buf, info->major, info->minor);
|
---|
| 1016 | break;
|
---|
| 1017 | case AC_MKNEWCOMPAT:
|
---|
| 1018 | 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);
|
---|
| 1023 |
|
---|
| 1024 | /* nothing found */
|
---|
| 1025 | if(i==0 || i > 9)
|
---|
| 1026 | return;
|
---|
| 1027 |
|
---|
| 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));
|
---|
| 1030 | dest_name = dest_buf;
|
---|
| 1031 | compat_name = compat_buf;
|
---|
| 1032 |
|
---|
| 1033 |
|
---|
| 1034 | /* 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);
|
---|
| 1037 |
|
---|
| 1038 | /* 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) );
|
---|
| 1041 |
|
---|
| 1042 | /* 5 == scsi/mt */
|
---|
| 1043 | if( i == 5)
|
---|
| 1044 | {
|
---|
| 1045 | rewind_ = info->devname[info->namelen - 1];
|
---|
| 1046 | if (rewind_ != 'n')
|
---|
| 1047 | rewind_ = '\0';
|
---|
| 1048 | mode=0;
|
---|
| 1049 | if(ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
|
---|
| 1050 | mode = ptr[2] - 107; /* 1 or 2 */
|
---|
| 1051 | if(ptr[2] == 'a')
|
---|
| 1052 | mode = 3;
|
---|
| 1053 | sprintf (compat_buf, fmt [i], host, bus, target, lun, mode, rewind_);
|
---|
| 1054 | }
|
---|
| 1055 |
|
---|
| 1056 | /* 9 == ide/host/mt */
|
---|
| 1057 | if( i == 9 )
|
---|
| 1058 | snprintf (compat_buf, sizeof (compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
|
---|
| 1059 | /* esac */
|
---|
| 1060 | } /* switch(action) */
|
---|
| 1061 |
|
---|
| 1062 | if(compat_name == NULL )
|
---|
| 1063 | return;
|
---|
| 1064 |
|
---|
| 1065 | debug_msg_logger( LOG_INFO, "%s: %s", __FUNCTION__, compat_name);
|
---|
| 1066 |
|
---|
| 1067 | /* Now decide what to do with it */
|
---|
| 1068 | switch (action)
|
---|
| 1069 | {
|
---|
| 1070 | case AC_MKOLDCOMPAT:
|
---|
| 1071 | case AC_MKNEWCOMPAT:
|
---|
| 1072 | mksymlink (dest_name, compat_name);
|
---|
| 1073 | break;
|
---|
| 1074 | case AC_RMOLDCOMPAT:
|
---|
| 1075 | case AC_RMNEWCOMPAT:
|
---|
| 1076 | ret = unlink (compat_name);
|
---|
| 1077 | if (ENABLE_DEBUG && ret)
|
---|
| 1078 | debug_msg_logger(LOG_ERR, "unlink: %s: %m", compat_name);
|
---|
| 1079 | break;
|
---|
| 1080 | /*esac*/
|
---|
| 1081 | } /* switch(action) */
|
---|
| 1082 | } /* End Function action_compat */
|
---|
| 1083 |
|
---|
| 1084 | static void restore(char *spath, struct stat source_stat, int rootlen)
|
---|
| 1085 | {
|
---|
| 1086 | char dpath[STRING_LENGTH];
|
---|
| 1087 | struct stat dest_stat;
|
---|
| 1088 |
|
---|
| 1089 | debug_msg_logger(LOG_INFO, __FUNCTION__);
|
---|
| 1090 |
|
---|
| 1091 | dest_stat.st_mode = 0;
|
---|
| 1092 | snprintf (dpath, sizeof dpath, "%s%s", mount_point, spath + rootlen);
|
---|
| 1093 | lstat (dpath, &dest_stat);
|
---|
| 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) )
|
---|
| 1099 | dir_operation(RESTORE, spath, rootlen,NULL);
|
---|
| 1100 | }
|
---|
| 1101 |
|
---|
| 1102 |
|
---|
| 1103 | static int copy_inode (const char *destpath, const struct stat *dest_stat,
|
---|
| 1104 | mode_t new_mode,
|
---|
| 1105 | const char *sourcepath, const struct stat *source_stat)
|
---|
| 1106 | /* [SUMMARY] Copy an inode.
|
---|
| 1107 | <destpath> The destination path. An existing inode may be deleted.
|
---|
| 1108 | <dest_stat> The destination stat(2) information.
|
---|
| 1109 | <new_mode> The desired new mode for the destination.
|
---|
| 1110 | <sourcepath> The source path.
|
---|
| 1111 | <source_stat> The source stat(2) information.
|
---|
| 1112 | [RETURNS] TRUE on success, else FALSE.
|
---|
| 1113 | */
|
---|
| 1114 | {
|
---|
| 1115 | int source_len, dest_len;
|
---|
| 1116 | char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
|
---|
| 1117 | int fd, val;
|
---|
| 1118 | struct sockaddr_un un_addr;
|
---|
| 1119 | char symlink_val[STRING_LENGTH];
|
---|
| 1120 |
|
---|
| 1121 | debug_msg_logger(LOG_INFO, __FUNCTION__);
|
---|
| 1122 |
|
---|
| 1123 | if ( (source_stat->st_mode & S_IFMT) == (dest_stat->st_mode & S_IFMT) )
|
---|
| 1124 | {
|
---|
| 1125 | /* 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);
|
---|
| 1131 | source_link[source_len] = '\0';
|
---|
| 1132 | 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);
|
---|
| 1137 | }
|
---|
| 1138 | return (TRUE);
|
---|
| 1139 | } /* Else not a symlink */
|
---|
| 1140 | chmod (destpath, new_mode & ~S_IFMT);
|
---|
| 1141 | chown (destpath, source_stat->st_uid, source_stat->st_gid);
|
---|
| 1142 | return (TRUE);
|
---|
| 1143 | }
|
---|
| 1144 | /* Different types: unlink and create */
|
---|
| 1145 | unlink (destpath);
|
---|
| 1146 | switch (source_stat->st_mode & S_IFMT)
|
---|
| 1147 | {
|
---|
| 1148 | case S_IFSOCK:
|
---|
| 1149 | if ( ( fd = socket (AF_UNIX, SOCK_STREAM, 0) ) < 0 )
|
---|
| 1150 | break;
|
---|
| 1151 | 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 (fd);
|
---|
| 1155 | if (val != 0 || chmod (destpath, new_mode & ~S_IFMT) != 0)
|
---|
| 1156 | break;
|
---|
| 1157 | goto do_chown;
|
---|
| 1158 | case S_IFLNK:
|
---|
| 1159 | if ( ( val = readlink (sourcepath, symlink_val, STRING_LENGTH - 1) ) < 0 )
|
---|
| 1160 | break;
|
---|
| 1161 | symlink_val[val] = '\0';
|
---|
| 1162 | if (symlink (symlink_val, destpath) == 0)
|
---|
| 1163 | return (TRUE);
|
---|
| 1164 | break;
|
---|
| 1165 | case S_IFREG:
|
---|
| 1166 | if ( ( fd = open (destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT) ) < 0 )
|
---|
| 1167 | break;
|
---|
| 1168 | close (fd);
|
---|
| 1169 | if (chmod (destpath, new_mode & ~S_IFMT) != 0)
|
---|
| 1170 | break;
|
---|
| 1171 | goto do_chown;
|
---|
| 1172 | case S_IFBLK:
|
---|
| 1173 | case S_IFCHR:
|
---|
| 1174 | case S_IFIFO:
|
---|
| 1175 | if (mknod (destpath, new_mode, source_stat->st_rdev) != 0)
|
---|
| 1176 | break;
|
---|
| 1177 | goto do_chown;
|
---|
| 1178 | case S_IFDIR:
|
---|
| 1179 | if (mkdir (destpath, new_mode & ~S_IFMT) != 0)
|
---|
| 1180 | break;
|
---|
| 1181 | do_chown:
|
---|
| 1182 | if (chown (destpath, source_stat->st_uid, source_stat->st_gid) == 0)
|
---|
| 1183 | return (TRUE);
|
---|
| 1184 | /*break;*/
|
---|
| 1185 | }
|
---|
| 1186 | return (FALSE);
|
---|
| 1187 | } /* End Function copy_inode */
|
---|
| 1188 |
|
---|
| 1189 | static void free_config (void)
|
---|
| 1190 | /* [SUMMARY] Free the configuration information.
|
---|
| 1191 | [RETURNS] Nothing.
|
---|
| 1192 | */
|
---|
| 1193 | {
|
---|
| 1194 | struct config_entry_struct *c_entry;
|
---|
| 1195 | void *next;
|
---|
| 1196 |
|
---|
| 1197 | debug_msg_logger(LOG_INFO, __FUNCTION__);
|
---|
| 1198 |
|
---|
| 1199 | for (c_entry = first_config; c_entry != NULL; c_entry = next)
|
---|
| 1200 | {
|
---|
| 1201 | unsigned int count;
|
---|
| 1202 |
|
---|
| 1203 | 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 | {
|
---|
| 1209 | if (c_entry->u.execute.argv[count] == NULL)
|
---|
| 1210 | break;
|
---|
| 1211 | free (c_entry->u.execute.argv[count]);
|
---|
| 1212 | }
|
---|
| 1213 | }
|
---|
| 1214 | free (c_entry);
|
---|
| 1215 | }
|
---|
| 1216 | first_config = NULL;
|
---|
| 1217 | last_config = NULL;
|
---|
| 1218 | } /* End Function free_config */
|
---|
| 1219 |
|
---|
| 1220 | static int get_uid_gid (int flag, const char *string)
|
---|
| 1221 | /* [SUMMARY] Convert a string to a UID or GID value.
|
---|
| 1222 | <flag> "UID" or "GID".
|
---|
| 1223 | <string> The string.
|
---|
| 1224 | [RETURNS] The UID or GID value.
|
---|
| 1225 | */
|
---|
| 1226 | {
|
---|
| 1227 | struct passwd *pw_ent;
|
---|
| 1228 | struct group *grp_ent;
|
---|
| 1229 | static char *msg;
|
---|
| 1230 |
|
---|
| 1231 | 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)
|
---|
| 1251 | msg_logger(LOG_ERR,"unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);
|
---|
| 1252 | return (0);
|
---|
| 1253 | }/* End Function get_uid_gid */
|
---|
| 1254 |
|
---|
| 1255 | static mode_t get_mode (const char *string)
|
---|
| 1256 | /* [SUMMARY] Convert a string to a mode value.
|
---|
| 1257 | <string> The string.
|
---|
| 1258 | [RETURNS] The mode value.
|
---|
| 1259 | */
|
---|
| 1260 | {
|
---|
| 1261 | mode_t mode;
|
---|
| 1262 | int i;
|
---|
| 1263 |
|
---|
| 1264 | debug_msg_logger(LOG_INFO, __FUNCTION__);
|
---|
| 1265 |
|
---|
| 1266 | if ( isdigit (string[0]) )
|
---|
| 1267 | return strtoul (string, NULL, 8);
|
---|
| 1268 | if (strlen (string) != 9)
|
---|
| 1269 | msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
|
---|
| 1270 |
|
---|
| 1271 | 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;
|
---|
| 1278 | string++;
|
---|
| 1279 | }
|
---|
| 1280 | return (mode);
|
---|
| 1281 | } /* End Function get_mode */
|
---|
| 1282 |
|
---|
| 1283 | static void signal_handler (int sig)
|
---|
| 1284 | {
|
---|
| 1285 | debug_msg_logger(LOG_INFO, __FUNCTION__);
|
---|
| 1286 |
|
---|
| 1287 | caught_signal = TRUE;
|
---|
| 1288 | if (sig == SIGHUP)
|
---|
| 1289 | caught_sighup = TRUE;
|
---|
| 1290 |
|
---|
| 1291 | msg_logger(LOG_INFO, "Caught signal %d", sig);
|
---|
| 1292 | } /* End Function signal_handler */
|
---|
| 1293 |
|
---|
| 1294 | static const char *get_variable (const char *variable, void *info)
|
---|
| 1295 | {
|
---|
| 1296 | 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 };
|
---|
| 1301 | int i;
|
---|
| 1302 |
|
---|
| 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 |
|
---|
| 1308 | /* 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);
|
---|
| 1332 | } /* End Function get_variable */
|
---|
| 1333 |
|
---|
| 1334 | static void service(struct stat statbuf, char *path)
|
---|
| 1335 | {
|
---|
| 1336 | struct devfsd_notify_struct info;
|
---|
| 1337 |
|
---|
| 1338 | debug_msg_logger(LOG_INFO, __FUNCTION__);
|
---|
| 1339 |
|
---|
| 1340 | memset (&info, 0, sizeof info);
|
---|
| 1341 | info.type = DEVFSD_NOTIFY_REGISTERED;
|
---|
| 1342 | info.mode = statbuf.st_mode;
|
---|
| 1343 | info.major = major (statbuf.st_rdev);
|
---|
| 1344 | info.minor = minor (statbuf.st_rdev);
|
---|
| 1345 | info.uid = statbuf.st_uid;
|
---|
| 1346 | info.gid = statbuf.st_gid;
|
---|
| 1347 | snprintf (info.devname, sizeof (info.devname), "%s", path + strlen (mount_point) + 1);
|
---|
| 1348 | info.namelen = strlen (info.devname);
|
---|
| 1349 | service_name (&info);
|
---|
| 1350 | if ( S_ISDIR (statbuf.st_mode) )
|
---|
| 1351 | dir_operation(SERVICE,path,0,NULL);
|
---|
| 1352 | }
|
---|
| 1353 |
|
---|
| 1354 | static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
|
---|
| 1355 | /* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
|
---|
| 1356 | <flag> To choose which function to perform
|
---|
| 1357 | <dp> The directory pointer. This is closed upon completion.
|
---|
| 1358 | <dir_name> The name of the directory.
|
---|
| 1359 | <rootlen> string length parameter.
|
---|
| 1360 | [RETURNS] Nothing.
|
---|
| 1361 | */
|
---|
| 1362 | {
|
---|
| 1363 | struct stat statbuf;
|
---|
| 1364 | DIR *dp;
|
---|
| 1365 | 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);
|
---|
| 1373 | 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])))
|
---|
| 1380 | 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;
|
---|
| 1388 | }
|
---|
| 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);
|
---|
| 1403 | } /* End Function do_scan_and_service */
|
---|
| 1404 |
|
---|
| 1405 | static int mksymlink (const char *oldpath, const char *newpath)
|
---|
| 1406 | /* [SUMMARY] Create a symlink, creating intervening directories as required.
|
---|
| 1407 | <oldpath> The string contained in the symlink.
|
---|
| 1408 | <newpath> The name of the new symlink.
|
---|
| 1409 | [RETURNS] 0 on success, else -1.
|
---|
| 1410 | */
|
---|
| 1411 | {
|
---|
| 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 | {
|
---|
| 1419 | 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);
|
---|
| 1426 | } /* End Function mksymlink */
|
---|
| 1427 |
|
---|
| 1428 |
|
---|
| 1429 | static int make_dir_tree (const char *path)
|
---|
| 1430 | /* [SUMMARY] Creating intervening directories for a path as required.
|
---|
| 1431 | <path> The full pathname (including the leaf node).
|
---|
| 1432 | [RETURNS] TRUE on success, else FALSE.
|
---|
| 1433 | */
|
---|
| 1434 | {
|
---|
| 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);
|
---|
| 1443 | } /* End Function make_dir_tree */
|
---|
| 1444 |
|
---|
| 1445 | static int expand_expression(char *output, unsigned int outsize,
|
---|
| 1446 | const char *input,
|
---|
| 1447 | const char *(*get_variable_func)(const char *variable, void *info),
|
---|
| 1448 | void *info,
|
---|
| 1449 | const char *devname,
|
---|
| 1450 | const regmatch_t *ex, unsigned int numexp)
|
---|
| 1451 | /* [SUMMARY] Expand environment variables and regular subexpressions in string.
|
---|
| 1452 | <output> The output expanded expression is written here.
|
---|
| 1453 | <length> The size of the output buffer.
|
---|
| 1454 | <input> The input expression. This may equal <<output>>.
|
---|
| 1455 | <get_variable> A function which will be used to get variable values. If
|
---|
| 1456 | this returns NULL, the environment is searched instead. If this is NULL,
|
---|
| 1457 | only the environment is searched.
|
---|
| 1458 | <info> An arbitrary pointer passed to <<get_variable>>.
|
---|
| 1459 | <devname> Device name; specifically, this is the string that contains all
|
---|
| 1460 | of the regular subexpressions.
|
---|
| 1461 | <ex> Array of start / end offsets into info->devname for each subexpression
|
---|
| 1462 | <numexp> Number of regular subexpressions found in <<devname>>.
|
---|
| 1463 | [RETURNS] TRUE on success, else FALSE.
|
---|
| 1464 | */
|
---|
| 1465 | {
|
---|
| 1466 | char temp[STRING_LENGTH];
|
---|
| 1467 |
|
---|
| 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);
|
---|
| 1474 | } /* End Function expand_expression */
|
---|
| 1475 |
|
---|
| 1476 | static void expand_regexp (char *output, size_t outsize, const char *input,
|
---|
| 1477 | const char *devname,
|
---|
| 1478 | const regmatch_t *ex, unsigned int numex )
|
---|
| 1479 | /* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
|
---|
| 1480 | <output> The output expanded expression is written here.
|
---|
| 1481 | <outsize> The size of the output buffer.
|
---|
| 1482 | <input> The input expression. This may NOT equal <<output>>, because
|
---|
| 1483 | supporting that would require yet another string-copy. However, it's not
|
---|
| 1484 | hard to write a simple wrapper function to add this functionality for those
|
---|
| 1485 | few cases that need it.
|
---|
| 1486 | <devname> Device name; specifically, this is the string that contains all
|
---|
| 1487 | of the regular subexpressions.
|
---|
| 1488 | <ex> An array of start and end offsets into <<devname>>, one for each
|
---|
| 1489 | subexpression
|
---|
| 1490 | <numex> Number of subexpressions in the offset-array <<ex>>.
|
---|
| 1491 | [RETURNS] Nothing.
|
---|
| 1492 | */
|
---|
| 1493 | {
|
---|
| 1494 | const char last_exp = '0' - 1 + numex;
|
---|
| 1495 | int c = -1;
|
---|
| 1496 |
|
---|
| 1497 | debug_msg_logger(LOG_INFO, __FUNCTION__);
|
---|
| 1498 |
|
---|
| 1499 | /* Guarantee NULL termination by writing an explicit '\0' character into
|
---|
| 1500 | the very last byte */
|
---|
| 1501 | if (outsize)
|
---|
| 1502 | output[--outsize] = '\0';
|
---|
| 1503 | /* Copy the input string into the output buffer, replacing '\\' with '\'
|
---|
| 1504 | and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
|
---|
| 1505 | codes are deleted */
|
---|
| 1506 | while ( (c != '\0') && (outsize != 0) )
|
---|
| 1507 | {
|
---|
| 1508 | c = *input;
|
---|
| 1509 | ++input;
|
---|
| 1510 | if (c == '\\')
|
---|
| 1511 | {
|
---|
| 1512 | c = *input;
|
---|
| 1513 | ++input;
|
---|
| 1514 | if (c != '\\')
|
---|
| 1515 | {
|
---|
| 1516 | if ((c >= '0') && (c <= last_exp))
|
---|
| 1517 | {
|
---|
| 1518 | const regmatch_t *subexp = ex + (c - '0');
|
---|
| 1519 | unsigned int sublen = subexp->rm_eo - subexp->rm_so;
|
---|
| 1520 |
|
---|
| 1521 | /* Range checking */
|
---|
| 1522 | if (sublen > outsize)
|
---|
| 1523 | sublen = outsize;
|
---|
| 1524 | strncpy (output, devname + subexp->rm_so, sublen);
|
---|
| 1525 | output += sublen;
|
---|
| 1526 | outsize -= sublen;
|
---|
| 1527 | }
|
---|
| 1528 | continue;
|
---|
| 1529 | }
|
---|
| 1530 | }
|
---|
| 1531 | *output = c;
|
---|
| 1532 | ++output;
|
---|
| 1533 | --outsize;
|
---|
| 1534 | } /* while */
|
---|
| 1535 | } /* End Function expand_regexp */
|
---|
| 1536 |
|
---|
| 1537 |
|
---|
| 1538 | /* from compat_name.c */
|
---|
| 1539 |
|
---|
| 1540 | struct translate_struct
|
---|
| 1541 | {
|
---|
| 1542 | char *match; /* The string to match to (up to length) */
|
---|
| 1543 | char *format; /* Format of output, "%s" takes data past match string,
|
---|
| 1544 | NULL is effectively "%s" (just more efficient) */
|
---|
| 1545 | };
|
---|
| 1546 |
|
---|
| 1547 | static struct translate_struct translate_table[] =
|
---|
| 1548 | {
|
---|
| 1549 | {"sound/", NULL},
|
---|
| 1550 | {"printers/", "lp%s"},
|
---|
| 1551 | {"v4l/", NULL},
|
---|
| 1552 | {"parports/", "parport%s"},
|
---|
| 1553 | {"fb/", "fb%s"},
|
---|
| 1554 | {"netlink/", NULL},
|
---|
| 1555 | {"loop/", "loop%s"},
|
---|
| 1556 | {"floppy/", "fd%s"},
|
---|
| 1557 | {"rd/", "ram%s"},
|
---|
| 1558 | {"md/", "md%s"}, /* Meta-devices */
|
---|
| 1559 | {"vc/", "tty%s"},
|
---|
| 1560 | {"misc/", NULL},
|
---|
| 1561 | {"isdn/", NULL},
|
---|
| 1562 | {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
|
---|
| 1563 | {"i2c/", "i2c-%s"},
|
---|
| 1564 | {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
|
---|
| 1565 | {"tts/E", "ttyE%s"}, /* Stallion serial driver */
|
---|
| 1566 | {"cua/E", "cue%s"}, /* Stallion serial driver callout */
|
---|
| 1567 | {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
|
---|
| 1568 | {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
|
---|
| 1569 | {"ip2/", "ip2%s"}, /* Computone serial driver control */
|
---|
| 1570 | {"tts/F", "ttyF%s"}, /* Computone serial driver */
|
---|
| 1571 | {"cua/F", "cuf%s"}, /* Computone serial driver callout */
|
---|
| 1572 | {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
|
---|
| 1573 | {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
|
---|
| 1574 | {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
|
---|
| 1575 | {"cua/", "cua%s"}, /* Generic serial: must be after others */
|
---|
| 1576 | {"input/js", "js%s"}, /* Joystick driver */
|
---|
| 1577 | {NULL, NULL}
|
---|
| 1578 | };
|
---|
| 1579 |
|
---|
| 1580 | const char *get_old_name (const char *devname, unsigned int namelen,
|
---|
| 1581 | char *buffer, unsigned int major, unsigned int minor)
|
---|
| 1582 | /* [SUMMARY] Translate a kernel-supplied name into an old name.
|
---|
| 1583 | <devname> The device name provided by the kernel.
|
---|
| 1584 | <namelen> The length of the name.
|
---|
| 1585 | <buffer> A buffer that may be used. This should be at least 128 bytes long.
|
---|
| 1586 | <major> The major number for the device.
|
---|
| 1587 | <minor> The minor number for the device.
|
---|
| 1588 | [RETURNS] A pointer to the old name if known, else NULL.
|
---|
| 1589 | */
|
---|
| 1590 | {
|
---|
| 1591 | const char *compat_name = NULL;
|
---|
| 1592 | char *ptr;
|
---|
| 1593 | struct translate_struct *trans;
|
---|
| 1594 | unsigned int i;
|
---|
| 1595 | char mode;
|
---|
| 1596 | int indexx;
|
---|
| 1597 | const char *pty1;
|
---|
| 1598 | const char *pty2;
|
---|
| 1599 | size_t len;
|
---|
| 1600 | /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
|
---|
| 1601 | static const char *const fmt[] = {
|
---|
| 1602 | NULL ,
|
---|
| 1603 | "sg%u", /* scsi/generic */
|
---|
| 1604 | NULL, /* scsi/disc */
|
---|
| 1605 | "sr%u", /* scsi/cd */
|
---|
| 1606 | NULL, /* scsi/part */
|
---|
| 1607 | "nst%u%c", /* scsi/mt */
|
---|
| 1608 | "hd%c" , /* ide/host/disc */
|
---|
| 1609 | "hd%c" , /* ide/host/cd */
|
---|
| 1610 | "hd%c%s", /* ide/host/part */
|
---|
| 1611 | "%sht%d", /* ide/host/mt */
|
---|
| 1612 | "sbpcd%u", /* sbp/ */
|
---|
| 1613 | "vcs%s", /* vcc/ */
|
---|
| 1614 | "%cty%c%c", /* pty/ */
|
---|
| 1615 | NULL
|
---|
| 1616 | };
|
---|
| 1617 |
|
---|
| 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 | {
|
---|
| 1626 | if (trans->format == NULL)
|
---|
| 1627 | return (devname + len);
|
---|
| 1628 | sprintf (buffer, trans->format, devname + len);
|
---|
| 1629 | return (buffer);
|
---|
| 1630 | }
|
---|
| 1631 | }
|
---|
| 1632 |
|
---|
| 1633 | ptr = (strrchr (devname, '/') + 1);
|
---|
| 1634 | i = scan_dev_name(devname, namelen, ptr);
|
---|
| 1635 |
|
---|
| 1636 | if( i > 0 && i < 13)
|
---|
| 1637 | compat_name = buffer;
|
---|
| 1638 | else
|
---|
| 1639 | return NULL;
|
---|
| 1640 |
|
---|
| 1641 | debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i);
|
---|
| 1642 |
|
---|
| 1643 | /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
|
---|
| 1644 | if( i == 1 || i == 3 || i == 10 )
|
---|
| 1645 | sprintf (buffer, fmt[i], minor);
|
---|
| 1646 |
|
---|
| 1647 | /* 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)));
|
---|
| 1650 |
|
---|
| 1651 | /* 5 == scsi/mt */
|
---|
| 1652 | if( i == 5)
|
---|
| 1653 | {
|
---|
| 1654 | mode = ptr[2];
|
---|
| 1655 | if (mode == 'n')
|
---|
| 1656 | mode = '\0';
|
---|
| 1657 | sprintf (buffer, fmt[i], minor & 0x1f, mode);
|
---|
| 1658 | if (devname[namelen - 1] != 'n')
|
---|
| 1659 | ++compat_name;
|
---|
| 1660 | }
|
---|
| 1661 | /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
|
---|
| 1662 | if( i == 6 || i == 7 || i == 8 )
|
---|
| 1663 | /* last arg should be ignored for i == 6 or i== 7 */
|
---|
| 1664 | sprintf (buffer, fmt[i] , get_old_ide_name (major, minor), ptr + 4);
|
---|
| 1665 |
|
---|
| 1666 | /* 9 == ide/host/mt */
|
---|
| 1667 | if( i == 9 )
|
---|
| 1668 | sprintf (buffer, fmt[i], ptr + 2, minor & 0x7f);
|
---|
| 1669 |
|
---|
| 1670 | /* 11 == vcc/ */
|
---|
| 1671 | if( i == 11 )
|
---|
| 1672 | {
|
---|
| 1673 | sprintf (buffer, fmt[i], devname + 4);
|
---|
| 1674 | if (buffer[3] == '0')
|
---|
| 1675 | buffer[3] = '\0';
|
---|
| 1676 | }
|
---|
| 1677 | /* 12 == pty/ */
|
---|
| 1678 | if( i == 12 )
|
---|
| 1679 | {
|
---|
| 1680 | pty1 = "pqrstuvwxyzabcde";
|
---|
| 1681 | 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);
|
---|
| 1690 | } /* End Function get_old_name */
|
---|
| 1691 |
|
---|
| 1692 | static char get_old_ide_name (unsigned int major, unsigned int minor)
|
---|
| 1693 | /* [SUMMARY] Get the old IDE name for a device.
|
---|
| 1694 | <major> The major number for the device.
|
---|
| 1695 | <minor> The minor number for the device.
|
---|
| 1696 | [RETURNS] The drive letter.
|
---|
| 1697 | */
|
---|
| 1698 | {
|
---|
| 1699 | char letter='y'; /* 121 */
|
---|
| 1700 | char c='a'; /* 97 */
|
---|
| 1701 | int i=IDE0_MAJOR;
|
---|
| 1702 |
|
---|
| 1703 | debug_msg_logger(LOG_INFO, __FUNCTION__);
|
---|
| 1704 |
|
---|
| 1705 | /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
|
---|
| 1706 | 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;
|
---|
| 1715 | break;
|
---|
| 1716 | }
|
---|
| 1717 | c+=2;
|
---|
| 1718 | }
|
---|
| 1719 | i++;
|
---|
| 1720 | } while(i<=IDE9_MAJOR);
|
---|
| 1721 |
|
---|
| 1722 | if (minor > 63)
|
---|
| 1723 | ++letter;
|
---|
| 1724 | return (letter);
|
---|
| 1725 | } /* End Function get_old_ide_name */
|
---|
| 1726 |
|
---|
| 1727 | static char *write_old_sd_name (char *buffer,
|
---|
| 1728 | unsigned int major, unsigned int minor,
|
---|
| 1729 | char *part)
|
---|
| 1730 | /* [SUMMARY] Write the old SCSI disc name to a buffer.
|
---|
| 1731 | <buffer> The buffer to write to.
|
---|
| 1732 | <major> The major number for the device.
|
---|
| 1733 | <minor> The minor number for the device.
|
---|
| 1734 | <part> The partition string. Must be "" for a whole-disc entry.
|
---|
| 1735 | [RETURNS] A pointer to the buffer on success, else NULL.
|
---|
| 1736 | */
|
---|
| 1737 | {
|
---|
| 1738 | unsigned int disc_index;
|
---|
| 1739 |
|
---|
| 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);
|
---|
| 1750 | if (disc_index < 26)
|
---|
| 1751 | sprintf (buffer, "sd%c%s", 'a' + disc_index, part);
|
---|
| 1752 | 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);
|
---|
| 1757 | } /* End Function write_old_sd_name */
|
---|
| 1758 |
|
---|
| 1759 |
|
---|
| 1760 | /* expression.c */
|
---|
| 1761 |
|
---|
| 1762 | /*EXPERIMENTAL_FUNCTION*/
|
---|
| 1763 |
|
---|
| 1764 | int st_expr_expand (char *output, unsigned int length, const char *input,
|
---|
| 1765 | const char *(*get_variable_func) (const char *variable,
|
---|
| 1766 | void *info),
|
---|
| 1767 | void *info)
|
---|
| 1768 | /* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
|
---|
| 1769 | <output> The output expanded expression is written here.
|
---|
| 1770 | <length> The size of the output buffer.
|
---|
| 1771 | <input> The input expression. This may equal <<output>>.
|
---|
| 1772 | <get_variable> A function which will be used to get variable values. If
|
---|
| 1773 | this returns NULL, the environment is searched instead. If this is NULL,
|
---|
| 1774 | only the environment is searched.
|
---|
| 1775 | <info> An arbitrary pointer passed to <<get_variable>>.
|
---|
| 1776 | [RETURNS] TRUE on success, else FALSE.
|
---|
| 1777 | */
|
---|
| 1778 | {
|
---|
| 1779 | char ch;
|
---|
| 1780 | unsigned int len;
|
---|
| 1781 | unsigned int out_pos = 0;
|
---|
| 1782 | const char *env;
|
---|
| 1783 | const char *ptr;
|
---|
| 1784 | struct passwd *pwent;
|
---|
| 1785 | char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
|
---|
| 1786 |
|
---|
| 1787 | debug_msg_logger(LOG_INFO, __FUNCTION__);
|
---|
| 1788 |
|
---|
| 1789 | if (length > BUFFER_SIZE)
|
---|
| 1790 | length = BUFFER_SIZE;
|
---|
| 1791 | for (; TRUE; ++input)
|
---|
| 1792 | {
|
---|
| 1793 | switch (ch = *input)
|
---|
| 1794 | {
|
---|
| 1795 | case '$':
|
---|
| 1796 | /* Variable expansion */
|
---|
| 1797 | input = expand_variable (buffer, length, &out_pos, ++input, get_variable_func, info);
|
---|
| 1798 | if (input == NULL)
|
---|
| 1799 | return (FALSE);
|
---|
| 1800 | break;
|
---|
| 1801 | case '~':
|
---|
| 1802 | /* Home directory expansion */
|
---|
| 1803 | ch = input[1];
|
---|
| 1804 | if ( isspace (ch) || (ch == '/') || (ch == '\0') )
|
---|
| 1805 | {
|
---|
| 1806 | /* 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);
|
---|
| 1811 | }
|
---|
| 1812 | len = strlen (env);
|
---|
| 1813 | if (len + out_pos >= length)
|
---|
| 1814 | goto st_expr_expand_out;
|
---|
| 1815 | memcpy (buffer + out_pos, env, len + 1);
|
---|
| 1816 | out_pos += len;
|
---|
| 1817 | continue;
|
---|
| 1818 | }
|
---|
| 1819 | /* Someone else's home directory */
|
---|
| 1820 | for (ptr = ++input; !isspace (ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
|
---|
| 1821 | /* VOID */ ;
|
---|
| 1822 | len = ptr - input;
|
---|
| 1823 | if (len >= sizeof tmp)
|
---|
| 1824 | goto st_expr_expand_out;
|
---|
| 1825 | safe_memcpy (tmp, input, len);
|
---|
| 1826 | input = ptr - 1;
|
---|
| 1827 | if ( ( pwent = getpwnam (tmp) ) == NULL )
|
---|
| 1828 | {
|
---|
| 1829 | msg_logger(LOG_INFO, "no pwent for: %s", tmp);
|
---|
| 1830 | return (FALSE);
|
---|
| 1831 | }
|
---|
| 1832 | len = strlen (pwent->pw_dir);
|
---|
| 1833 | if (len + out_pos >= length)
|
---|
| 1834 | goto st_expr_expand_out;
|
---|
| 1835 | memcpy (buffer + out_pos, pwent->pw_dir, len + 1);
|
---|
| 1836 | out_pos += len;
|
---|
| 1837 | break;
|
---|
| 1838 | case '\0':
|
---|
| 1839 | /* Falltrough */
|
---|
| 1840 | default:
|
---|
| 1841 | if (out_pos >= length)
|
---|
| 1842 | goto st_expr_expand_out;
|
---|
| 1843 | buffer[out_pos++] = ch;
|
---|
| 1844 | if (ch == '\0')
|
---|
| 1845 | {
|
---|
| 1846 | memcpy (output, buffer, out_pos);
|
---|
| 1847 | return (TRUE);
|
---|
| 1848 | }
|
---|
| 1849 | break;
|
---|
| 1850 | /* esac */
|
---|
| 1851 | }
|
---|
| 1852 | }
|
---|
| 1853 | return (FALSE);
|
---|
| 1854 | st_expr_expand_out:
|
---|
| 1855 | msg_logger(LOG_INFO, bb_msg_small_buffer);
|
---|
| 1856 | return (FALSE);
|
---|
| 1857 | } /* End Function st_expr_expand */
|
---|
| 1858 |
|
---|
| 1859 |
|
---|
| 1860 | /* Private functions follow */
|
---|
| 1861 |
|
---|
| 1862 | static const char *expand_variable (char *buffer, unsigned int length,
|
---|
| 1863 | unsigned int *out_pos, const char *input,
|
---|
| 1864 | const char *(*func) (const char *variable,
|
---|
| 1865 | void *info),
|
---|
| 1866 | void *info)
|
---|
| 1867 | /* [SUMMARY] Expand a variable.
|
---|
| 1868 | <buffer> The buffer to write to.
|
---|
| 1869 | <length> The length of the output buffer.
|
---|
| 1870 | <out_pos> The current output position. This is updated.
|
---|
| 1871 | <input> A pointer to the input character pointer.
|
---|
| 1872 | <func> A function which will be used to get variable values. If this
|
---|
| 1873 | returns NULL, the environment is searched instead. If this is NULL, only
|
---|
| 1874 | the environment is searched.
|
---|
| 1875 | <info> An arbitrary pointer passed to <<func>>.
|
---|
| 1876 | <errfp> Diagnostic messages are written here.
|
---|
| 1877 | [RETURNS] A pointer to the end of this subexpression on success, else NULL.
|
---|
| 1878 | */
|
---|
| 1879 | {
|
---|
| 1880 | char ch;
|
---|
| 1881 | int len;
|
---|
| 1882 | unsigned int open_braces;
|
---|
| 1883 | const char *env, *ptr;
|
---|
| 1884 | char tmp[STRING_LENGTH];
|
---|
| 1885 |
|
---|
| 1886 | debug_msg_logger(LOG_INFO, __FUNCTION__);
|
---|
| 1887 |
|
---|
| 1888 | ch = input[0];
|
---|
| 1889 | if (ch == '$')
|
---|
| 1890 | {
|
---|
| 1891 | /* Special case for "$$": PID */
|
---|
| 1892 | sprintf ( tmp, "%d", (int) getpid () );
|
---|
| 1893 | len = strlen (tmp);
|
---|
| 1894 | if (len + *out_pos >= length)
|
---|
| 1895 | goto expand_variable_out;
|
---|
| 1896 |
|
---|
| 1897 | memcpy (buffer + *out_pos, tmp, len + 1);
|
---|
| 1898 | out_pos += len;
|
---|
| 1899 | return (input);
|
---|
| 1900 | }
|
---|
| 1901 | /* Ordinary variable expansion, possibly in braces */
|
---|
| 1902 | if (ch != '{')
|
---|
| 1903 | {
|
---|
| 1904 | /* Simple variable expansion */
|
---|
| 1905 | for (ptr = input; isalnum (ch) || (ch == '_') || (ch == ':');ch = *++ptr)
|
---|
| 1906 | /* VOID */ ;
|
---|
| 1907 | len = ptr - input;
|
---|
| 1908 | if ((size_t)len >= sizeof tmp)
|
---|
| 1909 | goto expand_variable_out;
|
---|
| 1910 |
|
---|
| 1911 | safe_memcpy (tmp, input, len);
|
---|
| 1912 | 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);
|
---|
| 1917 | }
|
---|
| 1918 | len = strlen (env);
|
---|
| 1919 | if (len + *out_pos >= length)
|
---|
| 1920 | goto expand_variable_out;
|
---|
| 1921 |
|
---|
| 1922 | memcpy (buffer + *out_pos, env, len + 1);
|
---|
| 1923 | *out_pos += len;
|
---|
| 1924 | return (input);
|
---|
| 1925 | }
|
---|
| 1926 | /* Variable in braces: check for ':' tricks */
|
---|
| 1927 | ch = *++input;
|
---|
| 1928 | for (ptr = input; isalnum (ch) || (ch == '_'); ch = *++ptr)
|
---|
| 1929 | /* VOID */;
|
---|
| 1930 | if (ch == '}')
|
---|
| 1931 | {
|
---|
| 1932 | /* Must be simple variable expansion with "${var}" */
|
---|
| 1933 | len = ptr - input;
|
---|
| 1934 | if ((size_t)len >= sizeof tmp)
|
---|
| 1935 | goto expand_variable_out;
|
---|
| 1936 |
|
---|
| 1937 | safe_memcpy (tmp, input, len);
|
---|
| 1938 | ptr = expand_variable (buffer, length, out_pos, tmp, func, info );
|
---|
| 1939 | 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);
|
---|
| 1947 | }
|
---|
| 1948 | /* It's that handy "${var:-word}" expression. Check if var is defined */
|
---|
| 1949 | len = ptr - input;
|
---|
| 1950 | if ((size_t)len >= sizeof tmp)
|
---|
| 1951 | goto expand_variable_out;
|
---|
| 1952 |
|
---|
| 1953 | safe_memcpy (tmp, input, len);
|
---|
| 1954 | /* Move input pointer to ':' */
|
---|
| 1955 | input = ptr;
|
---|
| 1956 | /* First skip to closing brace, taking note of nested expressions */
|
---|
| 1957 | ptr += 2;
|
---|
| 1958 | ch = ptr[0];
|
---|
| 1959 | for (open_braces = 1; open_braces > 0; ch = *++ptr)
|
---|
| 1960 | {
|
---|
| 1961 | switch (ch)
|
---|
| 1962 | {
|
---|
| 1963 | case '{':
|
---|
| 1964 | ++open_braces;
|
---|
| 1965 | break;
|
---|
| 1966 | case '}':
|
---|
| 1967 | --open_braces;
|
---|
| 1968 | break;
|
---|
| 1969 | case '\0':
|
---|
| 1970 | msg_logger(LOG_INFO,"\"}\" not found in: %s", input);
|
---|
| 1971 | return (NULL);
|
---|
| 1972 | default:
|
---|
| 1973 | break;
|
---|
| 1974 | }
|
---|
| 1975 | }
|
---|
| 1976 | --ptr;
|
---|
| 1977 | /* At this point ptr should point to closing brace of "${var:-word}" */
|
---|
| 1978 | if ( ( env = get_variable_v2 (tmp, func, info) ) != NULL )
|
---|
| 1979 | {
|
---|
| 1980 | /* Found environment variable, so skip the input to the closing brace
|
---|
| 1981 | and return the variable */
|
---|
| 1982 | input = ptr;
|
---|
| 1983 | len = strlen (env);
|
---|
| 1984 | if (len + *out_pos >= length)
|
---|
| 1985 | goto expand_variable_out;
|
---|
| 1986 |
|
---|
| 1987 | memcpy (buffer + *out_pos, env, len + 1);
|
---|
| 1988 | *out_pos += len;
|
---|
| 1989 | return (input);
|
---|
| 1990 | }
|
---|
| 1991 | /* Environment variable was not found, so process word. Advance input
|
---|
| 1992 | pointer to start of word in "${var:-word}" */
|
---|
| 1993 | input += 2;
|
---|
| 1994 | len = ptr - input;
|
---|
| 1995 | if ((size_t)len >= sizeof tmp)
|
---|
| 1996 | goto expand_variable_out;
|
---|
| 1997 |
|
---|
| 1998 | safe_memcpy (tmp, input, len);
|
---|
| 1999 | input = ptr;
|
---|
| 2000 | if ( !st_expr_expand (tmp, STRING_LENGTH, tmp, func, info ) )
|
---|
| 2001 | return (NULL);
|
---|
| 2002 | len = strlen (tmp);
|
---|
| 2003 | if (len + *out_pos >= length)
|
---|
| 2004 | goto expand_variable_out;
|
---|
| 2005 |
|
---|
| 2006 | memcpy (buffer + *out_pos, tmp, len + 1);
|
---|
| 2007 | *out_pos += len;
|
---|
| 2008 | return (input);
|
---|
| 2009 | expand_variable_out:
|
---|
| 2010 | msg_logger(LOG_INFO, bb_msg_small_buffer);
|
---|
| 2011 | return (NULL);
|
---|
| 2012 | } /* End Function expand_variable */
|
---|
| 2013 |
|
---|
| 2014 |
|
---|
| 2015 | static const char *get_variable_v2 (const char *variable,
|
---|
| 2016 | const char *(*func) (const char *variable, void *info),
|
---|
| 2017 | void *info)
|
---|
| 2018 | /* [SUMMARY] Get a variable from the environment or .
|
---|
| 2019 | <variable> The variable name.
|
---|
| 2020 | <func> A function which will be used to get the variable. If this returns
|
---|
| 2021 | NULL, the environment is searched instead. If this is NULL, only the
|
---|
| 2022 | environment is searched.
|
---|
| 2023 | [RETURNS] The value of the variable on success, else NULL.
|
---|
| 2024 | */
|
---|
| 2025 | {
|
---|
| 2026 | const char *value;
|
---|
| 2027 |
|
---|
| 2028 | debug_msg_logger(LOG_INFO, __FUNCTION__);
|
---|
| 2029 |
|
---|
| 2030 | if (func != NULL)
|
---|
| 2031 | {
|
---|
| 2032 | value = (*func) (variable, info);
|
---|
| 2033 | if (value != NULL)
|
---|
| 2034 | return (value);
|
---|
| 2035 | }
|
---|
| 2036 | return getenv (variable);
|
---|
| 2037 | } /* End Function get_variable */
|
---|
| 2038 |
|
---|
| 2039 | /* END OF CODE */
|
---|