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