xref: /illumos-gate/usr/src/cmd/devfsadm/sensor_link.c (revision 5328fc53d11d7151861fa272e4fb0248b8f0e145)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2019, Joyent, Inc.
14  */
15 
16 /*
17  * Create /devices links for various sensors. The sensor series of node types
18  * all begin with ddi_sensor. After which, there is a series of : delineated
19  * paths in the node type. Those represent the directory under /dev/sensors that
20  * the nodes should ultimately be created.
21  *
22  * For example, ddi_sensor:temperature:cpu would cause us to place the named
23  * minor under /dev/sensors/temperature/cpu/. Currently it is up to drivers to
24  * not conflict in names or if there is a fear of conflicting, make sure their
25  * minor is unique.
26  */
27 
28 #include <devfsadm.h>
29 #include <string.h>
30 
31 #define	SENSORS_BASE	"sensors"
32 
33 static int
34 sensor_link(di_minor_t minor, di_node_t node)
35 {
36 	const char *t, *minor_name, *dir_path = NULL;
37 	char *type, *c;
38 	char buf[PATH_MAX];
39 	size_t len;
40 
41 	if ((t = di_minor_nodetype(minor)) == NULL) {
42 		return (DEVFSADM_CONTINUE);
43 	}
44 
45 	if ((minor_name = di_minor_name(minor)) == NULL) {
46 		return (DEVFSADM_CONTINUE);
47 	}
48 
49 	if ((type = strdup(t)) == NULL) {
50 		return (DEVFSADM_TERMINATE);
51 	}
52 
53 	while ((c = strchr(type, ':')) != NULL) {
54 		if (dir_path == NULL) {
55 			dir_path = c + 1;
56 		}
57 		*c = '/';
58 	}
59 
60 	if (dir_path == NULL || *dir_path == '\0') {
61 		len = snprintf(buf, sizeof (buf), "%s/%s", SENSORS_BASE,
62 		    minor_name);
63 	} else {
64 		len = snprintf(buf, sizeof (buf), "%s/%s/%s", SENSORS_BASE,
65 		    dir_path, minor_name);
66 	}
67 
68 	if (len < sizeof (buf)) {
69 		(void) devfsadm_mklink(buf, node, minor, 0);
70 	}
71 
72 	free(type);
73 	return (DEVFSADM_CONTINUE);
74 }
75 
76 static devfsadm_create_t sensor_create_cbt[] = {
77 	{ NULL, "ddi_sensor", NULL, TYPE_PARTIAL, ILEVEL_0, sensor_link }
78 };
79 DEVFSADM_CREATE_INIT_V0(sensor_create_cbt);
80