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