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