1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 * 21 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 22 * Use is subject to license terms. 23 */ 24 25 #ifndef _DEVFSADM_IMPL_H 26 #define _DEVFSADM_IMPL_H 27 28 #pragma ident "%Z%%M% %I% %E% SMI" 29 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 #include <dlfcn.h> 35 #include <stdarg.h> 36 #include <fcntl.h> 37 #include <sys/file.h> 38 #include <locale.h> 39 #include <libintl.h> 40 #include <ctype.h> 41 #include <signal.h> 42 #include <deflt.h> 43 #include <ftw.h> 44 #include <sys/instance.h> 45 #include <sys/types.h> 46 #include <dirent.h> 47 #include <pwd.h> 48 #include <grp.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <sys/mkdev.h> 52 #include <sys/stat.h> 53 #include <fcntl.h> 54 #include <errno.h> 55 #include <unistd.h> 56 #include <sys/stat.h> 57 #include <sys/int_types.h> 58 #include <limits.h> 59 #include <strings.h> 60 #include <devfsadm.h> 61 #include <libdevinfo.h> 62 #include <sys/devinfo_impl.h> 63 #include <sys/modctl.h> 64 #include <libgen.h> 65 #include <sys/hwconf.h> 66 #include <sys/sunddi.h> 67 #include <door.h> 68 #include <syslog.h> 69 #include <libsysevent.h> 70 #include <thread.h> 71 #include <message.h> 72 #include <sys/cladm.h> 73 #include <librcm.h> 74 #include <sys/sysevent/dev.h> 75 #include <libzonecfg.h> 76 #include <device_info.h> 77 #include <sys/fs/sdev_node.h> 78 #include <sys/syscall.h> 79 #include <rpcsvc/ypclnt.h> 80 #include <sys/sysevent/eventdefs.h> 81 82 #define DEV_LOCK_FILE ".devfsadm_dev.lock" 83 #define DAEMON_LOCK_FILE ".devfsadm_daemon.lock" 84 85 #define DEV "/dev" 86 #define ETC "/etc" 87 #define ETCDEV "/etc/dev" 88 #define DEV_LEN 4 89 #define DEVICES "/devices" 90 #define DEVICES_LEN 8 91 #define MODULE_DIRS "/usr/lib/devfsadm/linkmod" 92 #define ALIASFILE "/etc/driver_aliases" 93 #define NAME_TO_MAJOR "/etc/name_to_major" 94 #define RECONFIG_BOOT "_INIT_RECONFIG" 95 #define PID_STR_LEN 10 96 #define EXTRA_PRIVS "/etc/security/extra_privs" 97 #define DEV_POLICY "/etc/security/device_policy" 98 #define LDEV_FILE "/etc/logindevperm" 99 100 #define DEVFSADM_DEFAULT_FILE "/etc/default/devfsadm" 101 102 #define MINOR_FINI_TIMEOUT_DEFAULT 2 103 104 #define SYNCH_DOOR_PERMS (S_IRUSR | S_IWUSR) 105 106 #define DRVCONFIG "drvconfig" 107 #define DEVFSADM "devfsadm" 108 #define DEVFSADMD "devfsadmd" 109 #define DEVLINKS "devlinks" 110 #define TAPES "tapes" 111 #define AUDLINKS "audlinks" 112 #define PORTS "ports" 113 #define DISKS "disks" 114 115 #define MAX_IDLE_DELAY 5 116 #define MAX_DELAY 30 117 #define NAME 0x01 118 #define ADDR 0x03 119 #define MINOR 0x04 120 #define COUNTER 0x05 121 #define CONSTANT 0x06 122 #define TYPE 0x07 123 #define TYPE_S "type" 124 #define ADDR_S "addr" 125 #define ADDR_S_LEN 4 126 #define MINOR_S "minor" 127 #define MINOR_S_LEN 5 128 #define NAME_S "name" 129 #define TAB '\t' 130 #define NEWLINE '\n' 131 #define MAX_DEVLINK_LINE 4028 132 #define INTEGER 0 133 #define LETTER 1 134 #define MAX_PERM_LINE 256 135 #define MAX_LDEV_LINE 256 136 #define LDEV_DELIMS " \t\n" 137 #define LDEV_DRVLIST_DELIMS "=" 138 #define LDEV_DRV_DELIMS ", \t\n" 139 #define LDEV_DEV_DELIM ":" 140 #define LDEV_DRVLIST_NAME "driver" 141 #define NFP_HASH_SZ 256 142 143 #define TYPE_LINK 0x00 144 #define TYPE_DEVICES 0x01 145 146 #define CREATE_LINK 0x01 147 #define READ_LINK 0x02 148 #define CREATE_NODE 0x01 149 #define READ_NODE 0x02 150 151 #define CACHE_STATE 0x0 152 #define SYNC_STATE 0x1 153 154 #define MODULE_ACTIVE 0x01 155 156 /* Possible flag values for flag member of numeral_t */ 157 #define NUMERAL_RESERVED 0x01 158 159 #define MAX_SLEEP 120 160 161 #define DEVLINKTAB_FILE "/etc/devlink.tab" 162 163 #define MODULE_SUFFIX ".so" 164 #define MINOR_INIT "minor_init" 165 #define MINOR_FINI "minor_fini" 166 #define _DEVFSADM_CREATE_REG "_devfsadm_create_reg" 167 #define _DEVFSADM_REMOVE_REG "_devfsadm_remove_reg" 168 169 #define NUM_EV_STR 4 170 #define EV_TYPE 0 171 #define EV_CLASS 1 172 #define EV_PATH_NAME 2 173 #define EV_MINOR_NAME 3 174 175 /* add new debug level and meanings here */ 176 #define DEVLINK_MID "devfsadm:devlink" 177 #define MODLOAD_MID "devfsadm:modload" 178 #define INITFINI_MID "devfsadm:initfini" 179 #define EVENT_MID "devfsadm:event" 180 #define REMOVE_MID "devfsadm:remove" 181 #define LOCK_MID "devfsadm:lock" 182 #define PATH2INST_MID "devfsadm:path2inst" 183 #define CACHE_MID "devfsadm:cache" 184 #define BUILDCACHE_MID "devfsadm:buildcache" 185 #define RECURSEDEV_MID "devfsadm:recursedev" 186 #define INSTSYNC_MID "devfsadm:instsync" 187 #define FILES_MID "devfsadm:files" 188 #define ENUM_MID "devfsadm:enum" 189 #define RSRV_MID "devfsadm:rsrv" /* enum interface reserve */ 190 #define RSBY_MID "devfsadm:rsby" /* enum reserve bypass */ 191 #define LINKCACHE_MID "devfsadm:linkcache" 192 #define ADDREMCACHE_MID "devfsadm:addremcache" 193 #define MALLOC_MID "devfsadm:malloc" 194 #define READDIR_MID "devfsadm:readdir" 195 #define READDIR_ALL_MID "devfsadm:readdir_all" 196 #define DEVNAME_MID "devfsadm:devname" 197 #define ALL_MID "all" 198 199 #define DEVFSADM_DEBUG_ON (verbose == NULL) ? FALSE : TRUE 200 201 typedef struct recurse_dev { 202 void (*fcn)(char *, void *); 203 void *data; 204 } recurse_dev_t; 205 206 typedef struct link { 207 char *devlink; /* without ".../dev/" prefix */ 208 char *contents; /* without "../devices" prefix */ 209 struct link *next; 210 } link_t; 211 212 typedef struct linkhead { 213 regex_t dir_re_compiled; 214 char *dir_re; 215 link_t *link; 216 link_t *nextlink; 217 struct linkhead *nexthead; 218 } linkhead_t; 219 220 typedef struct link_list { 221 int type; 222 char *constant; 223 int arg; 224 struct link_list *next; 225 } link_list_t; 226 227 typedef struct selector_list { 228 int key; 229 char *val; 230 int arg; 231 struct selector_list *next; 232 } selector_list_t; 233 234 typedef struct devlinktab_list { 235 int line_number; 236 char *selector_pattern; 237 char *p_link_pattern; 238 char *s_link_pattern; 239 selector_list_t *selector; 240 link_list_t *p_link; 241 link_list_t *s_link; 242 struct devlinktab_list *next; 243 } devlinktab_list_t; 244 245 typedef struct module { 246 char *name; 247 void *dlhandle; 248 int (*minor_init)(); 249 int (*minor_fini)(); 250 int flags; 251 struct module *next; 252 } module_t; 253 254 typedef struct create_list { 255 devfsadm_create_t *create; 256 module_t *modptr; 257 regex_t node_type_comp; 258 regex_t drv_name_comp; 259 struct create_list *next; 260 } create_list_t; 261 262 struct minor { 263 di_node_t node; 264 di_minor_t minor; 265 struct minor *next; 266 }; 267 268 struct mlist { 269 struct minor *head; 270 struct minor *tail; 271 }; 272 273 typedef struct remove_list { 274 devfsadm_remove_V1_t *remove; 275 module_t *modptr; 276 struct remove_list *next; 277 } remove_list_t; 278 279 typedef struct item { 280 char *i_key; 281 struct item *i_next; 282 } item_t; 283 284 typedef struct cleanup_data { 285 int flags; 286 char *phypath; 287 remove_list_t *rm; 288 } cleanup_data_t; 289 290 typedef struct n2m { 291 major_t major; 292 char *driver; 293 struct n2m *next; 294 } n2m_t; 295 296 /* structures for devfsadm_enumerate() */ 297 typedef struct numeral { 298 char *id; 299 char *full_path; 300 int rule_index; 301 char *cmp_str; 302 struct numeral *next; 303 int flags; 304 } numeral_t; 305 306 typedef struct numeral_set { 307 int re_count; 308 char **re; 309 numeral_t *headnumeral; 310 struct numeral_set *next; 311 } numeral_set_t; 312 313 typedef struct temp { 314 int integer; 315 struct temp *next; 316 } temp_t; 317 318 typedef struct driver_alias { 319 char *driver_name; 320 char *alias_name; 321 struct driver_alias *next; 322 } driver_alias_t; 323 324 struct driver_list { 325 char driver_name[MAXNAMELEN]; 326 struct driver_list *next; 327 }; 328 329 struct login_dev { 330 char *ldev_console; 331 int ldev_perms; 332 char *ldev_device; 333 regex_t ldev_device_regex; 334 struct driver_list *ldev_driver_list; 335 struct login_dev *ldev_next; 336 }; 337 338 #define MAX_DEV_NAME_COUNT 100 339 struct devlink_cb_arg { 340 char *dev_names[MAX_DEV_NAME_COUNT]; 341 char *link_contents[MAX_DEV_NAME_COUNT]; 342 int count; 343 int rv; 344 }; 345 346 struct dca_impl { 347 char *dci_root; 348 char *dci_minor; 349 char *dci_driver; 350 void *dci_arg; 351 int dci_error; 352 int dci_flags; 353 }; 354 355 /* RCM related */ 356 struct rcm_eventq { 357 nvlist_t *nvl; 358 struct rcm_eventq *next; 359 }; 360 361 /* sysevent queue related */ 362 typedef struct syseventq_s { 363 struct syseventq_s *next; 364 char *class; 365 char *subclass; 366 nvlist_t *nvl; 367 } syseventq_t; 368 369 static int devfsadm_enumerate_int_start(char *devfs_path, 370 int index, char **buf, devfsadm_enumerate_t rules[], 371 int nrules, char *start); 372 static void set_root_devices_dev_dir(char *dir); 373 static void pre_and_post_cleanup(int flags); 374 static void hot_cleanup(char *, char *, char *, char *, int); 375 static void devfsadm_exit(int status); 376 static void rm_link_from_cache(char *devlink); 377 static void rm_all_links_from_cache(); 378 static void add_link_to_cache(char *devlink, char *physpath); 379 static linkhead_t *get_cached_links(char *dir_re); 380 static void build_devlink_list(char *check_link, void *data); 381 static void instance_flush_thread(void); 382 static int s_rmdir(char *path); 383 static void rm_parent_dir_if_empty(char *path); 384 static void free_link_list(link_list_t *head); 385 static void free_selector_list(selector_list_t *head); 386 void devfsadm_err_print(char *message, ...); 387 void defvsadm_print(int level, char *message, ...); 388 static int call_minor_init(module_t *module); 389 static void load_module(char *module, char *cdir); 390 static void invalidate_enumerate_cache(void); 391 static pid_t enter_dev_lock(void); 392 static void exit_dev_lock(int exiting); 393 static pid_t enter_daemon_lock(void); 394 static void exit_daemon_lock(int exiting); 395 static int process_devlink_compat(di_minor_t minor, di_node_t node); 396 static int alias(char *, char *); 397 static int devfsadm_copy(void); 398 static void flush_path_to_inst(void); 399 static void detachfromtty(void); 400 static void minor_process(di_node_t node, di_minor_t minor, 401 struct mlist *dep); 402 static void read_minor_perm_file(void); 403 static void read_driver_aliases_file(void); 404 static void load_modules(void); 405 static void unload_modules(void); 406 static void *s_malloc(const size_t size); 407 static void *s_zalloc(const size_t size); 408 static void devfs_instance_mod(void); 409 static void add_minor_pathname(char *, char *, char *); 410 static int check_minor_type(di_node_t node, di_minor_t minor, void *arg); 411 static void cache_deferred_minor(struct mlist *dep, di_node_t node, 412 di_minor_t minor); 413 static int compare_field(char *full_name, char *field_item, int field); 414 static int component_cat(char *link, char *name, int field); 415 static void recurse_dev_re(char *current_dir, char *path_re, recurse_dev_t *rd); 416 static void matching_dev(char *devpath, void *data); 417 static int resolve_link(char *devpath, char **content_p, int *type_p, 418 char **devfs_path, int dangle); 419 static int clean_ok(devfsadm_remove_V1_t *remove); 420 static int translate_major(dev_t old_dev, dev_t *new_dev); 421 static int get_major_no(char *driver, major_t *major); 422 static int load_n2m_table(char *filename); 423 static int get_stat_info(char *, struct stat *); 424 static char *new_id(numeral_t *, int, char *); 425 static int find_enum_id(devfsadm_enumerate_t rules[], int nrules, 426 char *devfs_path, int index, char *min, int type, char **buf, int multiple); 427 static void daemon_update(void); 428 static void usage(void); 429 static int getnexttoken(char *next, char **nextp, char **tokenpp, char *tchar); 430 static int class_ok(char *class); 431 static int create_link_common(char *devlink, char *contents, int *exists); 432 static char *dequote(char *src); 433 static void parse_args(int argc, char *argv[]); 434 static void process_devinfo_tree(void); 435 static void minor_fini_thread(void *arg); 436 static void *s_realloc(void *ptr, const size_t size); 437 static void read_devlinktab_file(void); 438 static selector_list_t *create_selector_list(char *selector); 439 static int parse_selector(char **selector, char **key, char **val); 440 int devfsadm_noupdate(void); 441 const char *devfsadm_root_path(void); 442 static link_list_t *create_link_list(char *link); 443 static void s_unlink(const char *file); 444 static void s_closedir(DIR *dirp); 445 static void s_mkdirp(const char *path, const mode_t mode); 446 static int is_minor_node(char *contents, char **mn_root); 447 static int construct_devlink(char *link, link_list_t *link_build, 448 char *contents, di_minor_t minor, 449 di_node_t node, char *pattern); 450 static int split_devlinktab_entry(char *entry, char **selector, char **p_link, 451 char **s_link); 452 static int devlink_matches(devlinktab_list_t *entry, di_minor_t minor, 453 di_node_t node); 454 static int build_links(devlinktab_list_t *entry, di_minor_t minor, 455 di_node_t node); 456 static numeral_set_t *get_enum_cache(devfsadm_enumerate_t rules[], 457 int nrules); 458 static void enumerate_recurse(char *current_dir, char *path_left, 459 numeral_set_t *setp, devfsadm_enumerate_t rules[], int index); 460 461 static int match_path_component(char *file_re, char *file, char **id, 462 int subexp); 463 static void create_cached_numeral(char *path, numeral_set_t *setp, 464 char *numeral_id, devfsadm_enumerate_t rules[], int index); 465 static int devfsadm_copy_file(const char *file, const struct stat *stat, 466 int flags, struct FTW *ftw); 467 static void getattr(char *devname, char *aminor, int spectype, dev_t dev, 468 mode_t *mode, uid_t *uid, gid_t *gid); 469 static int minor_matches_rule(di_node_t node, di_minor_t minor, 470 create_list_t *create); 471 static void add_verbose_id(char *mid); 472 static char *get_component(char *str, const char *comp_num); 473 static char *alloc_cmp_str(const char *devfs_path, devfsadm_enumerate_t *dep); 474 static int lookup_enum_cache(numeral_set_t *set, char *cmp_str, 475 devfsadm_enumerate_t rules[], int index, numeral_t **matchnpp); 476 static void sync_handler(void *cookie, char *ap, size_t asize, 477 door_desc_t *dp, uint_t ndesc); 478 static int zone_pathcheck(char *checkpath); 479 static void process_deferred_links(struct dca_impl *dcip, int flag); 480 static void event_handler(sysevent_t *ev); 481 static int rcm_init(void); 482 static int notify_rcm(di_node_t node, char *minor_name); 483 static void dca_impl_init(char *root, char *minor, struct dca_impl *dcip); 484 static void lock_dev(void); 485 static void unlock_dev(int flag); 486 static int devlink_cb(di_devlink_t dl, void *arg); 487 static void free_dev_names(struct devlink_cb_arg *x); 488 489 int load_devpolicy(void); 490 static void load_dev_acl(void); 491 static void load_minor_perm_file(void); 492 493 static int (*librcm_alloc_handle)(char *, uint_t, void *, rcm_handle_t **); 494 static void (*librcm_free_handle)(rcm_handle_t *); 495 static int (*librcm_notify_event)(rcm_handle_t *, char *, uint_t, nvlist_t *, 496 rcm_info_t *); 497 static nvlist_t *build_event_attributes(char *, char *, char *, 498 di_node_t, char *, int, char *); 499 static void log_event(char *, char *, nvlist_t *); 500 static void build_and_enq_event(char *, char *, char *, di_node_t, char *); 501 502 static void read_logindevperm_file(void); 503 static void set_logindev_perms(char *devlink); 504 505 static void reset_node_permissions(di_node_t, di_minor_t); 506 507 /* 508 * devname related 509 */ 510 static void devname_lookup_handler(void *, char *, size_t, 511 door_desc_t *, uint_t); /* /dev name lookup server */ 512 static int devname_kcall(int, void *); /* syscall into the devname fs */ 513 514 static void nfphash_create(void); 515 static int nfphash_fcn(char *key); 516 static item_t *nfphash_lookup(char *key); 517 static void nfphash_insert(char *key); 518 static void nfphash_destroy(void); 519 520 /* Enumerate reserve related */ 521 static void read_enumerate_file(void); 522 static int enumerate_parse(char *rsvstr, char *path_left, numeral_set_t *setp, 523 devfsadm_enumerate_t rules[], int index); 524 static void create_reserved_numeral(numeral_set_t *setp, char *numeral_id); 525 526 /* convenient short hands */ 527 #define vprint devfsadm_print 528 #define err_print devfsadm_errprint 529 #ifndef TRUE 530 #define TRUE 1 531 #endif 532 #ifndef FALSE 533 #define FALSE 0 534 #endif 535 536 #ifdef __cplusplus 537 } 538 #endif 539 540 #endif /* _DEVFSADM_IMPL_H */ 541