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