xref: /titanic_50/usr/src/cmd/devfsadm/devfsadm_impl.h (revision 9039eeaf76678a41be39feb97b4f724272ef7e18)
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/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 #undef	DEBUG
83 #ifndef DEBUG
84 #define	NDEBUG 1
85 #else
86 #undef	NDEBUG
87 #endif
88 
89 #include <assert.h>
90 
91 
92 #define	DEV_LOCK_FILE ".devfsadm_dev.lock"
93 #define	DAEMON_LOCK_FILE ".devfsadm_daemon.lock"
94 
95 #define	DEV "/dev"
96 #define	ETC "/etc"
97 #define	ETCDEV "/etc/dev"
98 #define	DEV_LEN 4
99 #define	DEVICES "/devices"
100 #define	DEVICES_LEN 8
101 #define	MODULE_DIRS "/usr/lib/devfsadm/linkmod"
102 #define	ALIASFILE "/etc/driver_aliases"
103 #define	NAME_TO_MAJOR "/etc/name_to_major"
104 #define	RECONFIG_BOOT "_INIT_RECONFIG"
105 #define	PID_STR_LEN 10
106 #define	EXTRA_PRIVS	"/etc/security/extra_privs"
107 #define	DEV_POLICY	"/etc/security/device_policy"
108 #define	LDEV_FILE	"/etc/logindevperm"
109 
110 #define	DEVFSADM_DEFAULT_FILE "/etc/default/devfsadm"
111 
112 #define	MINOR_FINI_TIMEOUT_DEFAULT 2
113 #define	FORCE_CALL_MINOR_FINI	10
114 
115 #define	SYNCH_DOOR_PERMS	(S_IRUSR | S_IWUSR)
116 
117 #define	DRVCONFIG "drvconfig"
118 #define	DEVFSADM "devfsadm"
119 #define	DEVFSADMD "devfsadmd"
120 #define	DEVLINKS "devlinks"
121 #define	TAPES "tapes"
122 #define	AUDLINKS "audlinks"
123 #define	PORTS "ports"
124 #define	DISKS "disks"
125 
126 #define	MAX_IDLE_DELAY 5
127 #define	MAX_DELAY 30
128 #define	NAME 0x01
129 #define	ADDR 0x03
130 #define	MINOR 0x04
131 #define	COUNTER 0x05
132 #define	CONSTANT 0x06
133 #define	TYPE 0x07
134 #define	TYPE_S "type"
135 #define	ADDR_S "addr"
136 #define	ADDR_S_LEN 4
137 #define	MINOR_S "minor"
138 #define	MINOR_S_LEN 5
139 #define	NAME_S "name"
140 #define	TAB '\t'
141 #define	NEWLINE '\n'
142 #define	MAX_DEVLINK_LINE 4028
143 #define	INTEGER 0
144 #define	LETTER 1
145 #define	MAX_PERM_LINE 256
146 #define	MAX_LDEV_LINE 256
147 #define	LDEV_DELIMS " \t\n"
148 #define	LDEV_DRVLIST_DELIMS "="
149 #define	LDEV_DRV_DELIMS ", \t\n"
150 #define	LDEV_DEV_DELIM ":"
151 #define	LDEV_DRVLIST_NAME "driver"
152 
153 #define	TYPE_LINK 0x00
154 #define	TYPE_DEVICES 0x01
155 
156 #define	CREATE_LINK 0x01
157 #define	READ_LINK 0x02
158 #define	CREATE_NODE 0x01
159 #define	READ_NODE 0x02
160 
161 #define	DCA_CREATE_LINK	0x01
162 #define	DCA_FREE_LIST 0x02
163 #define	DCA_LOAD_DRV 0x04
164 #define	DCA_CHECK_TYPE 0x10
165 #define	DCA_NOTIFY_RCM 0x20
166 #define	DCA_FLUSH_PATHINST 0x40
167 #define	DCA_HOT_PLUG 0x80
168 
169 #define	CACHE_STATE 0x0
170 #define	SYNC_STATE 0x1
171 
172 #define	MODULE_ACTIVE 0x01
173 
174 #define	MAX_SLEEP 120
175 
176 #define	DEVLINKTAB_FILE "/etc/devlink.tab"
177 
178 #define	MODULE_SUFFIX ".so"
179 #define	MINOR_INIT "minor_init"
180 #define	MINOR_FINI "minor_fini"
181 #define	_DEVFSADM_CREATE_REG "_devfsadm_create_reg"
182 #define	_DEVFSADM_REMOVE_REG "_devfsadm_remove_reg"
183 
184 #define	NUM_EV_STR		4
185 #define	EV_TYPE			0
186 #define	EV_CLASS		1
187 #define	EV_PATH_NAME		2
188 #define	EV_MINOR_NAME		3
189 
190 /* add new debug level and meanings here */
191 #define	DEVLINK_MID		"devfsadm:devlink"
192 #define	MODLOAD_MID		"devfsadm:modload"
193 #define	INITFINI_MID		"devfsadm:initfini"
194 #define	EVENT_MID		"devfsadm:event"
195 #define	REMOVE_MID		"devfsadm:remove"
196 #define	LOCK_MID		"devfsadm:lock"
197 #define	PATH2INST_MID		"devfsadm:path2inst"
198 #define	CACHE_MID		"devfsadm:cache"
199 #define	BUILDCACHE_MID		"devfsadm:buildcache"
200 #define	RECURSEDEV_MID		"devfsadm:recursedev"
201 #define	INSTSYNC_MID		"devfsadm:instsync"
202 #define	FILES_MID		"devfsadm:files"
203 #define	ENUM_MID		"devfsadm:enum"
204 #define	LINKCACHE_MID		"devfsadm:linkcache"
205 #define	ADDREMCACHE_MID		"devfsadm:addremcache"
206 #define	MALLOC_MID		"devfsadm:malloc"
207 #define	READDIR_MID		"devfsadm:readdir"
208 #define	READDIR_ALL_MID		"devfsadm:readdir_all"
209 #define	DEVNAME_MID		"devfsadm:devname"
210 #define	ALL_MID			"all"
211 
212 #define	DEVFSADM_DEBUG_ON	(verbose == NULL) ? FALSE : TRUE
213 
214 typedef struct recurse_dev {
215 	void (*fcn)(char *, void *);
216 	void *data;
217 } recurse_dev_t;
218 
219 typedef struct link {
220 	char *devlink; /* without ".../dev/"   prefix */
221 	char *contents; /* without "../devices" prefix */
222 	struct link *next;
223 } link_t;
224 
225 typedef struct linkhead {
226 	regex_t dir_re_compiled;
227 	char *dir_re;
228 	link_t *link;
229 	link_t *nextlink;
230 	struct linkhead *nexthead;
231 } linkhead_t;
232 
233 typedef struct link_list  {
234 	int type;
235 	char *constant;
236 	int arg;
237 	struct link_list *next;
238 } link_list_t;
239 
240 typedef struct selector_list {
241 	int key;
242 	char *val;
243 	int arg;
244 	struct selector_list *next;
245 } selector_list_t;
246 
247 typedef struct devlinktab_list {
248 	int line_number;
249 	char *selector_pattern;
250 	char *p_link_pattern;
251 	char *s_link_pattern;
252 	selector_list_t *selector;
253 	link_list_t *p_link;
254 	link_list_t *s_link;
255 	struct devlinktab_list *next;
256 } devlinktab_list_t;
257 
258 typedef struct module {
259 	char *name;
260 	void *dlhandle;
261 	int (*minor_init)();
262 	int (*minor_fini)();
263 	int flags;
264 	struct module *next;
265 } module_t;
266 
267 typedef struct create_list {
268 	devfsadm_create_t *create;
269 	module_t *modptr;
270 	regex_t node_type_comp;
271 	regex_t drv_name_comp;
272 	struct create_list *next;
273 } create_list_t;
274 
275 struct minor {
276 	di_node_t node;
277 	di_minor_t minor;
278 	struct minor *next;
279 };
280 
281 struct mlist {
282 	struct minor *head;
283 	struct minor *tail;
284 };
285 
286 typedef struct remove_list {
287 	devfsadm_remove_t *remove;
288 	module_t *modptr;
289 	struct remove_list *next;
290 } remove_list_t;
291 
292 typedef struct cleanup_data {
293 	int flags;
294 	char *phypath;
295 	remove_list_t *rm;
296 } cleanup_data_t;
297 
298 typedef struct n2m {
299 	major_t major;
300 	char *driver;
301 	struct n2m *next;
302 } n2m_t;
303 
304 /* structures for devfsadm_enumerate() */
305 typedef struct numeral {
306 	char *id;
307 	char *full_path;
308 	int rule_index;
309 	char *cmp_str;
310 	struct numeral *next;
311 } numeral_t;
312 
313 typedef struct numeral_set {
314 	int re_count;
315 	char **re;
316 	numeral_t *headnumeral;
317 	struct numeral_set *next;
318 } numeral_set_t;
319 
320 typedef struct temp {
321 	int integer;
322 	struct temp *next;
323 } temp_t;
324 
325 typedef struct driver_alias {
326 	char *driver_name;
327 	char *alias_name;
328 	struct driver_alias *next;
329 } driver_alias_t;
330 
331 struct driver_list {
332 	char driver_name[MAXNAMELEN];
333 	struct driver_list *next;
334 };
335 
336 struct login_dev {
337 	char *ldev_console;
338 	int ldev_perms;
339 	char *ldev_device;
340 	regex_t ldev_device_regex;
341 	struct driver_list *ldev_driver_list;
342 	struct login_dev *ldev_next;
343 };
344 
345 #define	MAX_DEV_NAME_COUNT	100
346 struct devlink_cb_arg {
347 	char *dev_names[MAX_DEV_NAME_COUNT];
348 	char *link_contents[MAX_DEV_NAME_COUNT];
349 	int count;
350 	int rv;
351 };
352 
353 struct dca_impl {
354 	char *dci_root;
355 	char *dci_minor;
356 	char *dci_driver;
357 	void *dci_arg;
358 	int dci_error;
359 	int dci_flags;
360 };
361 
362 /* RCM related */
363 struct rcm_eventq {
364 	nvlist_t *nvl;
365 	struct rcm_eventq *next;
366 };
367 
368 static int devfsadm_enumerate_int_start(char *devfs_path,
369 	int index, char **buf, devfsadm_enumerate_t rules[],
370 	int nrules, char *start);
371 static void startup_cache_sync_thread(void);
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(void);
393 static pid_t enter_daemon_lock(void);
394 static void exit_daemon_lock(void);
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_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 call_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);
499 static void log_event(char *, char *, nvlist_t *);
500 static void build_and_log_event(char *, char *, char *, di_node_t);
501 static char *dev_readdir(char *);
502 
503 static void read_logindevperm_file(void);
504 static void set_logindev_perms(char *devlink);
505 
506 static void reset_node_permissions(di_node_t, di_minor_t);
507 
508 /*
509  * devname related
510  */
511 static void devname_lookup_handler(void *, char *, size_t,
512     door_desc_t *, uint_t);		/* /dev name lookup server */
513 static int devname_kcall(int, void *);	/* syscall into the devname fs */
514 
515 /* convenient short hands */
516 #define	vprint		devfsadm_print
517 #define	err_print	devfsadm_errprint
518 #ifndef TRUE
519 #define	TRUE	1
520 #endif
521 #ifndef FALSE
522 #define	FALSE	0
523 #endif
524 
525 #ifdef	__cplusplus
526 }
527 #endif
528 
529 #endif /* _DEVFSADM_IMPL_H */
530