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