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.
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.
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]
21 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
22 * Use is subject to license terms.
25 #ifndef _DEVFSADM_IMPL_H
26 #define _DEVFSADM_IMPL_H
42 #include <sys/instance.h>
43 #include <sys/types.h>
49 #include <sys/mkdev.h>
55 #include <sys/int_types.h>
59 #include <libdevinfo.h>
60 #include <sys/devinfo_impl.h>
61 #include <sys/modctl.h>
63 #include <sys/hwconf.h>
64 #include <sys/sunddi.h>
67 #include <libsysevent.h>
70 #include <sys/sysevent/dev.h>
71 #include <libzonecfg.h>
72 #include <device_info.h>
73 #include <sys/fs/sdev_impl.h>
74 #include <sys/syscall.h>
75 #include <rpcsvc/ypclnt.h>
76 #include <sys/sysevent/eventdefs.h>
78 #define DEV_LOCK_FILE ".devfsadm_dev.lock"
79 #define DAEMON_LOCK_FILE ".devfsadm_daemon.lock"
83 #define ETCDEV "/etc/dev"
85 #define DEVICES "/devices"
87 #define MODULE_DIRS "/usr/lib/devfsadm/linkmod"
88 #define ALIASFILE "/etc/driver_aliases"
89 #define NAME_TO_MAJOR "/etc/name_to_major"
90 #define RECONFIG_BOOT "_INIT_RECONFIG"
91 #define PID_STR_LEN 10
92 #define EXTRA_PRIVS "/etc/security/extra_privs"
93 #define DEV_POLICY "/etc/security/device_policy"
94 #define LDEV_FILE "/etc/logindevperm"
96 #define DEVFSADM_DEFAULT_FILE "/etc/default/devfsadm"
98 #define MINOR_FINI_TIMEOUT_DEFAULT 2
100 #define SYNCH_DOOR_PERMS (S_IRUSR | S_IWUSR)
102 #define DRVCONFIG "drvconfig"
103 #define DEVFSADM "devfsadm"
104 #define DEVFSADMD "devfsadmd"
105 #define DEVLINKS "devlinks"
106 #define TAPES "tapes"
107 #define AUDLINKS "audlinks"
108 #define PORTS "ports"
109 #define DISKS "disks"
111 #define MAX_IDLE_DELAY 5
117 #define CONSTANT 0x06
119 #define TYPE_S "type"
120 #define ADDR_S "addr"
122 #define MINOR_S "minor"
123 #define MINOR_S_LEN 5
124 #define NAME_S "name"
127 #define MAX_DEVLINK_LINE 4028
130 #define MAX_PERM_LINE 256
131 #define MAX_LDEV_LINE 256
132 #define LDEV_DELIMS " \t\n"
133 #define LDEV_DRVLIST_DELIMS "="
134 #define LDEV_DRV_DELIMS ", \t\n"
135 #define LDEV_DEV_DELIM ":"
136 #define LDEV_DRVLIST_NAME "driver"
137 #define NFP_HASH_SZ 256
139 #define TYPE_LINK 0x00
140 #define TYPE_DEVICES 0x01
142 #define CREATE_LINK 0x01
143 #define READ_LINK 0x02
144 #define CREATE_NODE 0x01
145 #define READ_NODE 0x02
147 #define CACHE_STATE 0x0
148 #define SYNC_STATE 0x1
150 #define MODULE_ACTIVE 0x01
152 /* Possible flag values for flag member of numeral_t */
153 #define NUMERAL_RESERVED 0x01
155 #define MAX_SLEEP 120
157 #define DEVLINKTAB_FILE "/etc/devlink.tab"
159 #define MODULE_SUFFIX ".so"
160 #define MINOR_INIT "minor_init"
161 #define MINOR_FINI "minor_fini"
162 #define _DEVFSADM_CREATE_REG "_devfsadm_create_reg"
163 #define _DEVFSADM_REMOVE_REG "_devfsadm_remove_reg"
168 #define EV_PATH_NAME 2
169 #define EV_MINOR_NAME 3
171 /* add new debug level and meanings here */
172 #define DEVLINK_MID "devfsadm:devlink"
173 #define MODLOAD_MID "devfsadm:modload"
174 #define INITFINI_MID "devfsadm:initfini"
175 #define EVENT_MID "devfsadm:event"
176 #define REMOVE_MID "devfsadm:remove"
177 #define LOCK_MID "devfsadm:lock"
178 #define PATH2INST_MID "devfsadm:path2inst"
179 #define CACHE_MID "devfsadm:cache"
180 #define BUILDCACHE_MID "devfsadm:buildcache"
181 #define RECURSEDEV_MID "devfsadm:recursedev"
182 #define INSTSYNC_MID "devfsadm:instsync"
183 #define FILES_MID "devfsadm:files"
184 #define ENUM_MID "devfsadm:enum"
185 #define RSRV_MID "devfsadm:rsrv" /* enum interface reserve */
186 #define RSBY_MID "devfsadm:rsby" /* enum reserve bypass */
187 #define LINKCACHE_MID "devfsadm:linkcache"
188 #define ADDREMCACHE_MID "devfsadm:addremcache"
189 #define MALLOC_MID "devfsadm:malloc"
190 #define READDIR_MID "devfsadm:readdir"
191 #define READDIR_ALL_MID "devfsadm:readdir_all"
192 #define DEVNAME_MID "devfsadm:devname"
193 #define ALL_MID "all"
195 #define DEVFSADM_DEBUG_ON (verbose == NULL) ? FALSE : TRUE
197 typedef struct recurse_dev
{
198 void (*fcn
)(char *, void *);
202 typedef struct link
{
203 char *devlink
; /* without ".../dev/" prefix */
204 char *contents
; /* without "../devices" prefix */
208 typedef struct linkhead
{
209 regex_t dir_re_compiled
;
213 struct linkhead
*nexthead
;
216 typedef struct link_list
{
220 struct link_list
*next
;
223 typedef struct selector_list
{
227 struct selector_list
*next
;
230 typedef struct devlinktab_list
{
232 char *selector_pattern
;
233 char *p_link_pattern
;
234 char *s_link_pattern
;
235 selector_list_t
*selector
;
238 struct devlinktab_list
*next
;
241 typedef struct module
{
250 typedef struct create_list
{
251 devfsadm_create_t
*create
;
253 regex_t node_type_comp
;
254 regex_t drv_name_comp
;
255 struct create_list
*next
;
269 typedef struct remove_list
{
270 devfsadm_remove_V1_t
*remove
;
272 struct remove_list
*next
;
275 typedef struct item
{
280 typedef struct cleanup_data
{
292 /* structures for devfsadm_enumerate() */
293 typedef struct numeral
{
298 struct numeral
*next
;
302 typedef struct numeral_set
{
305 numeral_t
*headnumeral
;
306 struct numeral_set
*next
;
309 typedef struct temp
{
314 typedef struct driver_alias
{
317 struct driver_alias
*next
;
321 char driver_name
[MAXNAMELEN
];
322 struct driver_list
*next
;
329 regex_t ldev_device_regex
;
330 struct driver_list
*ldev_driver_list
;
331 struct login_dev
*ldev_next
;
334 #define MAX_DEV_NAME_COUNT 100
335 struct devlink_cb_arg
{
336 char *dev_names
[MAX_DEV_NAME_COUNT
];
337 char *link_contents
[MAX_DEV_NAME_COUNT
];
351 /* sysevent queue related */
352 typedef struct syseventq_s
{
353 struct syseventq_s
*next
;
359 static int devfsadm_enumerate_int_start(char *devfs_path
,
360 int index
, char **buf
, devfsadm_enumerate_t rules
[],
361 int nrules
, char *start
);
362 static void set_root_devices_dev_dir(char *dir
);
363 static void pre_and_post_cleanup(int flags
);
364 static void hot_cleanup(char *, char *, char *, char *, int);
365 static void devfsadm_exit(int status
);
366 static void rm_link_from_cache(char *devlink
);
367 static void rm_all_links_from_cache();
368 static void add_link_to_cache(char *devlink
, char *physpath
);
369 static linkhead_t
*get_cached_links(char *dir_re
);
370 static void build_devlink_list(char *check_link
, void *data
);
371 static void instance_flush_thread(void);
372 static int s_rmdir(char *path
);
373 static void rm_parent_dir_if_empty(char *path
);
374 static void free_link_list(link_list_t
*head
);
375 static void free_selector_list(selector_list_t
*head
);
376 void devfsadm_err_print(char *message
, ...);
377 void defvsadm_print(int level
, char *message
, ...);
378 static int call_minor_init(module_t
*module
);
379 static void load_module(char *module
, char *cdir
);
380 static void invalidate_enumerate_cache(void);
381 static pid_t
enter_dev_lock(void);
382 static void exit_dev_lock(int exiting
);
383 static pid_t
enter_daemon_lock(void);
384 static void exit_daemon_lock(int exiting
);
385 static int process_devlink_compat(di_minor_t minor
, di_node_t node
);
386 static int alias(char *, char *);
387 static int devfsadm_copy(void);
388 static void flush_path_to_inst(void);
389 static void detachfromtty(void);
390 static void minor_process(di_node_t node
, di_minor_t minor
,
392 static void read_minor_perm_file(void);
393 static void read_driver_aliases_file(void);
394 static void load_modules(void);
395 static void unload_modules(void);
396 static void *s_malloc(const size_t size
);
397 static void *s_zalloc(const size_t size
);
398 static void devfs_instance_mod(void);
399 static void add_minor_pathname(char *, char *, char *);
400 static int check_minor_type(di_node_t node
, di_minor_t minor
, void *arg
);
401 static void cache_deferred_minor(struct mlist
*dep
, di_node_t node
,
403 static int compare_field(char *full_name
, char *field_item
, int field
);
404 static int component_cat(char *link
, char *name
, int field
);
405 static void recurse_dev_re(char *current_dir
, char *path_re
, recurse_dev_t
*rd
);
406 static void matching_dev(char *devpath
, void *data
);
407 static int resolve_link(char *devpath
, char **content_p
, int *type_p
,
408 char **devfs_path
, int dangle
);
409 static int clean_ok(devfsadm_remove_V1_t
*remove
);
410 static int translate_major(dev_t old_dev
, dev_t
*new_dev
);
411 static int get_major_no(char *driver
, major_t
*major
);
412 static int load_n2m_table(char *filename
);
413 static int get_stat_info(char *, struct stat
*);
414 static char *new_id(numeral_t
*, int, char *);
415 static int find_enum_id(devfsadm_enumerate_t rules
[], int nrules
,
416 char *devfs_path
, int index
, char *min
, int type
, char **buf
, int multiple
);
417 static void daemon_update(void);
418 static void usage(void);
419 static int getnexttoken(char *next
, char **nextp
, char **tokenpp
, char *tchar
);
420 static int class_ok(char *class);
421 static int create_link_common(char *devlink
, char *contents
, int *exists
);
422 static char *dequote(char *src
);
423 static void parse_args(int argc
, char *argv
[]);
424 static void process_devinfo_tree(void);
425 static void minor_fini_thread(void *arg
);
426 static void *s_realloc(void *ptr
, const size_t size
);
427 static void read_devlinktab_file(void);
428 static selector_list_t
*create_selector_list(char *selector
);
429 static int parse_selector(char **selector
, char **key
, char **val
);
430 int devfsadm_noupdate(void);
431 const char *devfsadm_root_path(void);
432 static link_list_t
*create_link_list(char *link
);
433 static void s_unlink(const char *file
);
434 static void s_closedir(DIR *dirp
);
435 static void s_mkdirp(const char *path
, const mode_t mode
);
436 static int is_minor_node(char *contents
, char **mn_root
);
437 static int construct_devlink(char *link
, link_list_t
*link_build
,
438 char *contents
, di_minor_t minor
,
439 di_node_t node
, char *pattern
);
440 static int split_devlinktab_entry(char *entry
, char **selector
, char **p_link
,
442 static int devlink_matches(devlinktab_list_t
*entry
, di_minor_t minor
,
444 static int build_links(devlinktab_list_t
*entry
, di_minor_t minor
,
446 static numeral_set_t
*get_enum_cache(devfsadm_enumerate_t rules
[],
448 static void enumerate_recurse(char *current_dir
, char *path_left
,
449 numeral_set_t
*setp
, devfsadm_enumerate_t rules
[], int index
);
451 static int match_path_component(char *file_re
, char *file
, char **id
,
453 static void create_cached_numeral(char *path
, numeral_set_t
*setp
,
454 char *numeral_id
, devfsadm_enumerate_t rules
[], int index
);
455 static int devfsadm_copy_file(const char *file
, const struct stat
*stat
,
456 int flags
, struct FTW
*ftw
);
457 static void getattr(char *devname
, char *aminor
, int spectype
, dev_t dev
,
458 mode_t
*mode
, uid_t
*uid
, gid_t
*gid
);
459 static int minor_matches_rule(di_node_t node
, di_minor_t minor
,
460 create_list_t
*create
);
461 static void add_verbose_id(char *mid
);
462 static char *get_component(char *str
, const char *comp_num
);
463 static char *alloc_cmp_str(const char *devfs_path
, devfsadm_enumerate_t
*dep
);
464 static int lookup_enum_cache(numeral_set_t
*set
, char *cmp_str
,
465 devfsadm_enumerate_t rules
[], int index
, numeral_t
**matchnpp
);
466 static void sync_handler(void *cookie
, char *ap
, size_t asize
,
467 door_desc_t
*dp
, uint_t ndesc
);
468 static int zone_pathcheck(char *checkpath
);
469 static void process_deferred_links(struct dca_impl
*dcip
, int flag
);
470 static void event_handler(sysevent_t
*ev
);
471 static void dca_impl_init(char *root
, char *minor
, struct dca_impl
*dcip
);
472 static void lock_dev(void);
473 static void unlock_dev(int flag
);
474 static int devlink_cb(di_devlink_t dl
, void *arg
);
475 static void free_dev_names(struct devlink_cb_arg
*x
);
477 int load_devpolicy(void);
478 static void load_dev_acl(void);
479 static void load_minor_perm_file(void);
481 static nvlist_t
*build_event_attributes(char *, char *, char *,
482 di_node_t
, char *, int, char *);
483 static void log_event(char *, char *, nvlist_t
*);
484 static void build_and_enq_event(char *, char *, char *, di_node_t
, char *);
486 static void read_logindevperm_file(void);
487 static void set_logindev_perms(char *devlink
);
489 static void reset_node_permissions(di_node_t
, di_minor_t
);
494 static void devname_lookup_handler(void *, char *, size_t,
495 door_desc_t
*, uint_t
); /* /dev name lookup server */
496 static int devname_kcall(int, void *); /* syscall into the devname fs */
498 static void nfphash_create(void);
499 static int nfphash_fcn(char *key
);
500 static item_t
*nfphash_lookup(char *key
);
501 static void nfphash_insert(char *key
);
502 static void nfphash_destroy(void);
504 /* Enumerate reserve related */
505 static void read_enumerate_file(void);
506 static int enumerate_parse(char *rsvstr
, char *path_left
, numeral_set_t
*setp
,
507 devfsadm_enumerate_t rules
[], int index
);
508 static void create_reserved_numeral(numeral_set_t
*setp
, char *numeral_id
);
510 /* convenient short hands */
511 #define vprint devfsadm_print
512 #define err_print devfsadm_errprint
524 #endif /* _DEVFSADM_IMPL_H */