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