xref: /dragonfly/lib/libdevattr/devattr_device.c (revision 0c8103dd)
13a3826b3SAlex Hornung /*
23a3826b3SAlex Hornung  * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
33a3826b3SAlex Hornung  *
43a3826b3SAlex Hornung  * This code is derived from software contributed to The DragonFly Project
53a3826b3SAlex Hornung  * by Alex Hornung <ahornung@gmail.com>
63a3826b3SAlex Hornung  *
73a3826b3SAlex Hornung  * Redistribution and use in source and binary forms, with or without
83a3826b3SAlex Hornung  * modification, are permitted provided that the following conditions
93a3826b3SAlex Hornung  * are met:
103a3826b3SAlex Hornung  *
113a3826b3SAlex Hornung  * 1. Redistributions of source code must retain the above copyright
123a3826b3SAlex Hornung  *    notice, this list of conditions and the following disclaimer.
133a3826b3SAlex Hornung  * 2. Redistributions in binary form must reproduce the above copyright
143a3826b3SAlex Hornung  *    notice, this list of conditions and the following disclaimer in
153a3826b3SAlex Hornung  *    the documentation and/or other materials provided with the
163a3826b3SAlex Hornung  *    distribution.
173a3826b3SAlex Hornung  * 3. Neither the name of The DragonFly Project nor the names of its
183a3826b3SAlex Hornung  *    contributors may be used to endorse or promote products derived
193a3826b3SAlex Hornung  *    from this software without specific, prior written permission.
203a3826b3SAlex Hornung  *
213a3826b3SAlex Hornung  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
223a3826b3SAlex Hornung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
233a3826b3SAlex Hornung  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
243a3826b3SAlex Hornung  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
253a3826b3SAlex Hornung  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
263a3826b3SAlex Hornung  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
273a3826b3SAlex Hornung  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
283a3826b3SAlex Hornung  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
293a3826b3SAlex Hornung  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
303a3826b3SAlex Hornung  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
313a3826b3SAlex Hornung  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
323a3826b3SAlex Hornung  * SUCH DAMAGE.
333a3826b3SAlex Hornung  */
343a3826b3SAlex Hornung #include <sys/types.h>
353a3826b3SAlex Hornung #include <sys/device.h>
363a3826b3SAlex Hornung #include <sys/wait.h>
373a3826b3SAlex Hornung #include <sys/socket.h>
383a3826b3SAlex Hornung #include <sys/ioctl.h>
393a3826b3SAlex Hornung #include <sys/poll.h>
403a3826b3SAlex Hornung #include <sys/queue.h>
413a3826b3SAlex Hornung #include <sys/stat.h>
423a3826b3SAlex Hornung #include <sys/un.h>
433a3826b3SAlex Hornung 
443a3826b3SAlex Hornung #include <err.h>
453a3826b3SAlex Hornung #include <errno.h>
463a3826b3SAlex Hornung #include <fcntl.h>
47*0c8103ddSSascha Wildner #include <inttypes.h>
483a3826b3SAlex Hornung #include <libgen.h>
493a3826b3SAlex Hornung #include <regex.h>
503a3826b3SAlex Hornung #include <signal.h>
513a3826b3SAlex Hornung #include <stdarg.h>
523a3826b3SAlex Hornung #include <stdio.h>
533a3826b3SAlex Hornung #include <stdlib.h>
543a3826b3SAlex Hornung #include <string.h>
553a3826b3SAlex Hornung #include <syslog.h>
563a3826b3SAlex Hornung #include <unistd.h>
573a3826b3SAlex Hornung 
583a3826b3SAlex Hornung #include <libprop/proplib.h>
593a3826b3SAlex Hornung #include <sys/udev.h>
603bbbc629SAlex Hornung #define LIBDEVATTR_INTERNAL
613a3826b3SAlex Hornung #include "devattr.h"
623a3826b3SAlex Hornung 
633a3826b3SAlex Hornung struct udev_device {
643a3826b3SAlex Hornung 	struct udev	*udev_ctx;
653a3826b3SAlex Hornung 	prop_dictionary_t	dict;
663a3826b3SAlex Hornung 	int	ev_type;
673a3826b3SAlex Hornung 	int	refs;
683a3826b3SAlex Hornung };
693a3826b3SAlex Hornung 
703a3826b3SAlex Hornung struct udev_device *
udev_device_new_from_dictionary(struct udev * udev_ctx,prop_dictionary_t dict)713a3826b3SAlex Hornung udev_device_new_from_dictionary(struct udev *udev_ctx, prop_dictionary_t dict)
723a3826b3SAlex Hornung {
733a3826b3SAlex Hornung 	struct udev_device *udev_dev;
743a3826b3SAlex Hornung 
753a3826b3SAlex Hornung 	udev_dev = malloc(sizeof(struct udev_device));
763a3826b3SAlex Hornung 	if (udev_dev == NULL)
773a3826b3SAlex Hornung 		return NULL;
783a3826b3SAlex Hornung 
793a3826b3SAlex Hornung 	udev_dev->refs = 1;
803a3826b3SAlex Hornung 	udev_dev->ev_type = UDEV_EVENT_NONE;
813a3826b3SAlex Hornung 
823a3826b3SAlex Hornung 	if (dict != NULL)
833a3826b3SAlex Hornung 		prop_object_retain(dict);
843a3826b3SAlex Hornung 
853a3826b3SAlex Hornung 	udev_dev->dict = dict;
863a3826b3SAlex Hornung 	udev_dev->udev_ctx = udev_ref(udev_ctx);
873a3826b3SAlex Hornung 
883a3826b3SAlex Hornung 	return udev_dev;
893a3826b3SAlex Hornung }
903a3826b3SAlex Hornung 
913a3826b3SAlex Hornung struct udev_device *
udev_device_ref(struct udev_device * udev_device)923a3826b3SAlex Hornung udev_device_ref(struct udev_device *udev_device)
933a3826b3SAlex Hornung {
943a3826b3SAlex Hornung 	atomic_add_int(&udev_device->refs, 1);
953a3826b3SAlex Hornung 
963a3826b3SAlex Hornung 	return udev_device;
973a3826b3SAlex Hornung }
983a3826b3SAlex Hornung 
993a3826b3SAlex Hornung void
udev_device_unref(struct udev_device * udev_device)1003a3826b3SAlex Hornung udev_device_unref(struct udev_device *udev_device)
1013a3826b3SAlex Hornung {
1023a3826b3SAlex Hornung 	int refcount;
1033a3826b3SAlex Hornung 
1043a3826b3SAlex Hornung 	refcount = atomic_fetchadd_int(&udev_device->refs, -1);
1053a3826b3SAlex Hornung 
1063a3826b3SAlex Hornung 	if (refcount == 1) {
1073a3826b3SAlex Hornung 		atomic_subtract_int(&udev_device->refs, 0x400); /* in destruction */
1083a3826b3SAlex Hornung 		if (udev_device->dict != NULL)
1093a3826b3SAlex Hornung 			prop_object_release(udev_device->dict);
1103a3826b3SAlex Hornung 
1113a3826b3SAlex Hornung 		udev_unref(udev_device->udev_ctx);
1123a3826b3SAlex Hornung 		free(udev_device);
1133a3826b3SAlex Hornung 	}
1143a3826b3SAlex Hornung }
1153a3826b3SAlex Hornung 
1163a3826b3SAlex Hornung void
udev_device_set_action(struct udev_device * udev_device,int action)1173a3826b3SAlex Hornung udev_device_set_action(struct udev_device *udev_device, int action)
1183a3826b3SAlex Hornung {
1193a3826b3SAlex Hornung 	udev_device->ev_type = action;
1203a3826b3SAlex Hornung }
1213a3826b3SAlex Hornung 
1223a3826b3SAlex Hornung const char *
udev_device_get_action(struct udev_device * udev_device)1233a3826b3SAlex Hornung udev_device_get_action(struct udev_device *udev_device)
1243a3826b3SAlex Hornung {
1253a3826b3SAlex Hornung 	const char *action;
1263a3826b3SAlex Hornung 
1273a3826b3SAlex Hornung 	switch (udev_device->ev_type) {
1283a3826b3SAlex Hornung 	case UDEV_EVENT_ATTACH:
1293a3826b3SAlex Hornung 		action = "add";
1303a3826b3SAlex Hornung 		break;
1313a3826b3SAlex Hornung 
1323a3826b3SAlex Hornung 	case UDEV_EVENT_DETACH:
1333a3826b3SAlex Hornung 		action = "remove";
1343a3826b3SAlex Hornung 		break;
1353a3826b3SAlex Hornung 
1363a3826b3SAlex Hornung 	default:
1373a3826b3SAlex Hornung 		action = "none";
1383a3826b3SAlex Hornung 		break;
1393a3826b3SAlex Hornung 	}
1403a3826b3SAlex Hornung 
1413a3826b3SAlex Hornung 	return action;
1423a3826b3SAlex Hornung }
1433a3826b3SAlex Hornung 
1443a3826b3SAlex Hornung dev_t
udev_device_get_devnum(struct udev_device * udev_device)1453a3826b3SAlex Hornung udev_device_get_devnum(struct udev_device *udev_device)
1463a3826b3SAlex Hornung {
1473a3826b3SAlex Hornung 	prop_number_t pn;
1483a3826b3SAlex Hornung 	dev_t devnum;
1493a3826b3SAlex Hornung 
1503a3826b3SAlex Hornung 	if (udev_device->dict == NULL)
1513a3826b3SAlex Hornung 		return 0;
1523a3826b3SAlex Hornung 
1533a3826b3SAlex Hornung 	pn = prop_dictionary_get(udev_device->dict, "devnum");
1543a3826b3SAlex Hornung 	if (pn == NULL)
1553a3826b3SAlex Hornung 		return 0;
1563a3826b3SAlex Hornung 
1573a3826b3SAlex Hornung 	devnum = prop_number_unsigned_integer_value(pn);
1583a3826b3SAlex Hornung 
1593a3826b3SAlex Hornung 	return devnum;
1603a3826b3SAlex Hornung }
1613a3826b3SAlex Hornung 
1623a3826b3SAlex Hornung uint64_t
udev_device_get_kptr(struct udev_device * udev_device)1633a3826b3SAlex Hornung udev_device_get_kptr(struct udev_device *udev_device)
1643a3826b3SAlex Hornung {
1653a3826b3SAlex Hornung 	prop_number_t pn;
1663a3826b3SAlex Hornung 	uint64_t kptr;
1673a3826b3SAlex Hornung 
1683a3826b3SAlex Hornung 	if (udev_device->dict == NULL)
1693a3826b3SAlex Hornung 		return 0;
1703a3826b3SAlex Hornung 
1713a3826b3SAlex Hornung 	pn = prop_dictionary_get(udev_device->dict, "kptr");
1723a3826b3SAlex Hornung 	if (pn == NULL)
1733a3826b3SAlex Hornung 		return 0;
1743a3826b3SAlex Hornung 
1753a3826b3SAlex Hornung 	kptr = prop_number_unsigned_integer_value(pn);
1763a3826b3SAlex Hornung 
1773a3826b3SAlex Hornung 	return kptr;
1783a3826b3SAlex Hornung }
1793a3826b3SAlex Hornung 
1803a3826b3SAlex Hornung int32_t
udev_device_get_major(struct udev_device * udev_device)1813a3826b3SAlex Hornung udev_device_get_major(struct udev_device *udev_device)
1823a3826b3SAlex Hornung {
1833a3826b3SAlex Hornung 	prop_number_t pn;
1843a3826b3SAlex Hornung 	int32_t major;
1853a3826b3SAlex Hornung 
1863a3826b3SAlex Hornung 	if (udev_device->dict == NULL)
1873a3826b3SAlex Hornung 		return 0;
1883a3826b3SAlex Hornung 
1893a3826b3SAlex Hornung 	pn = prop_dictionary_get(udev_device->dict, "major");
1903a3826b3SAlex Hornung 	if (pn == NULL)
1913a3826b3SAlex Hornung 		return 0;
1923a3826b3SAlex Hornung 
1933a3826b3SAlex Hornung 	major = (int32_t)prop_number_integer_value(pn);
1943a3826b3SAlex Hornung 
1953a3826b3SAlex Hornung 	return major;
1963a3826b3SAlex Hornung }
1973a3826b3SAlex Hornung 
1983a3826b3SAlex Hornung int32_t
udev_device_get_minor(struct udev_device * udev_device)1993a3826b3SAlex Hornung udev_device_get_minor(struct udev_device *udev_device)
2003a3826b3SAlex Hornung {
2013a3826b3SAlex Hornung 	prop_number_t pn;
2023a3826b3SAlex Hornung 	int32_t minor;
2033a3826b3SAlex Hornung 
2043a3826b3SAlex Hornung 	if (udev_device->dict == NULL)
2053a3826b3SAlex Hornung 		return 0;
2063a3826b3SAlex Hornung 
2073a3826b3SAlex Hornung 	pn = prop_dictionary_get(udev_device->dict, "minor");
2083a3826b3SAlex Hornung 	if (pn == NULL)
2093a3826b3SAlex Hornung 		return 0;
2103a3826b3SAlex Hornung 
2113a3826b3SAlex Hornung 	minor = (int32_t)prop_number_integer_value(pn);
2123a3826b3SAlex Hornung 
2133a3826b3SAlex Hornung 	return minor;
2143a3826b3SAlex Hornung }
2153a3826b3SAlex Hornung 
2163a3826b3SAlex Hornung const char *
udev_device_get_devnode(struct udev_device * udev_device)2173a3826b3SAlex Hornung udev_device_get_devnode(struct udev_device *udev_device)
2183a3826b3SAlex Hornung {
2193a3826b3SAlex Hornung 	dev_t devnum;
2203a3826b3SAlex Hornung 
2213a3826b3SAlex Hornung 	devnum = udev_device_get_devnum(udev_device);
2223a3826b3SAlex Hornung 	if (devnum == 0)
2233a3826b3SAlex Hornung 		return 0;
2243a3826b3SAlex Hornung 
2253a3826b3SAlex Hornung 	return devname(devnum, S_IFCHR);
2263a3826b3SAlex Hornung }
2273a3826b3SAlex Hornung 
2283a3826b3SAlex Hornung const char *
udev_device_get_property_value(struct udev_device * udev_device,const char * key)2293a3826b3SAlex Hornung udev_device_get_property_value(struct udev_device *udev_device,
2303a3826b3SAlex Hornung 				const char *key)
2313a3826b3SAlex Hornung {
2323a3826b3SAlex Hornung 	prop_object_t	po;
2333a3826b3SAlex Hornung 	prop_number_t	pn;
2343a3826b3SAlex Hornung 	prop_string_t	ps;
2353a3826b3SAlex Hornung 	static char buf[128]; /* XXX: might cause trouble */
2363a3826b3SAlex Hornung 	const char *str = NULL;
2373a3826b3SAlex Hornung 
2383a3826b3SAlex Hornung 	if (udev_device->dict == NULL)
2393a3826b3SAlex Hornung 		return NULL;
2403a3826b3SAlex Hornung 
2413a3826b3SAlex Hornung 	if ((po = prop_dictionary_get(udev_device->dict, key)) == NULL)
2423a3826b3SAlex Hornung 		return NULL;
2433a3826b3SAlex Hornung 
2443a3826b3SAlex Hornung 	if (prop_object_type(po) == PROP_TYPE_STRING) {
2453a3826b3SAlex Hornung 		ps = po;
2463a3826b3SAlex Hornung 		str = __DECONST(char *, prop_string_cstring_nocopy(ps));
2473a3826b3SAlex Hornung 	} else if (prop_object_type(po) == PROP_TYPE_NUMBER) {
2483a3826b3SAlex Hornung 		pn = po;
2493a3826b3SAlex Hornung 		if (prop_number_unsigned(pn)) {
2500ec7fe45SSascha Wildner 			snprintf(buf, sizeof(buf), "%" PRIu64,
2510ec7fe45SSascha Wildner 			    prop_number_unsigned_integer_value(pn));
2523a3826b3SAlex Hornung 		} else {
2530ec7fe45SSascha Wildner 			snprintf(buf, sizeof(buf), "%" PRIi64,
2540ec7fe45SSascha Wildner 			    prop_number_integer_value(pn));
2553a3826b3SAlex Hornung 		}
2563a3826b3SAlex Hornung 		str = buf;
2573a3826b3SAlex Hornung 	}
2583a3826b3SAlex Hornung 	return str;
2593a3826b3SAlex Hornung }
2603a3826b3SAlex Hornung 
2613a3826b3SAlex Hornung const char *
udev_device_get_subsystem(struct udev_device * udev_device)2623a3826b3SAlex Hornung udev_device_get_subsystem(struct udev_device *udev_device)
2633a3826b3SAlex Hornung {
2643a3826b3SAlex Hornung 	return udev_device_get_property_value(udev_device, "subsystem");
2653a3826b3SAlex Hornung }
2663a3826b3SAlex Hornung 
2673a3826b3SAlex Hornung const char *
udev_device_get_driver(struct udev_device * udev_device)2683a3826b3SAlex Hornung udev_device_get_driver(struct udev_device *udev_device)
2693a3826b3SAlex Hornung {
2703a3826b3SAlex Hornung 	return udev_device_get_property_value(udev_device, "driver");
2713a3826b3SAlex Hornung }
2723a3826b3SAlex Hornung 
2733a3826b3SAlex Hornung prop_dictionary_t
udev_device_get_dictionary(struct udev_device * udev_device)2743a3826b3SAlex Hornung udev_device_get_dictionary(struct udev_device *udev_device)
2753a3826b3SAlex Hornung {
2763a3826b3SAlex Hornung 	return udev_device->dict;
2773a3826b3SAlex Hornung }
2783a3826b3SAlex Hornung 
2793a3826b3SAlex Hornung struct udev *
udev_device_get_udev(struct udev_device * udev_device)2803a3826b3SAlex Hornung udev_device_get_udev(struct udev_device *udev_device)
2813a3826b3SAlex Hornung {
2823a3826b3SAlex Hornung 	return udev_device->udev_ctx;
2833a3826b3SAlex Hornung }
2843a3826b3SAlex Hornung 
285