1 /* 2 * Copyright (c) 2010 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Alex Hornung <ahornung@gmail.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 #include <sys/types.h> 35 #include <sys/device.h> 36 #include <sys/wait.h> 37 #include <sys/socket.h> 38 #include <sys/ioctl.h> 39 #include <sys/poll.h> 40 #include <sys/queue.h> 41 #include <sys/stat.h> 42 #include <sys/un.h> 43 #include <cpu/inttypes.h> 44 45 #include <err.h> 46 #include <errno.h> 47 #include <fcntl.h> 48 #include <libgen.h> 49 #include <regex.h> 50 #include <signal.h> 51 #include <stdarg.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <syslog.h> 56 #include <unistd.h> 57 #include <pthread.h> 58 59 #include <libprop/proplib.h> 60 #include <sys/udev.h> 61 #define LIBDEVATTR_INTERNAL 62 #include "devattr.h" 63 64 struct udev_device { 65 struct udev *udev_ctx; 66 prop_dictionary_t dict; 67 int ev_type; 68 int refs; 69 }; 70 71 struct udev_device * 72 udev_device_new_from_dictionary(struct udev *udev_ctx, prop_dictionary_t dict) 73 { 74 struct udev_device *udev_dev; 75 76 udev_dev = malloc(sizeof(struct udev_device)); 77 if (udev_dev == NULL) 78 return NULL; 79 80 udev_dev->refs = 1; 81 udev_dev->ev_type = UDEV_EVENT_NONE; 82 83 if (dict != NULL) 84 prop_object_retain(dict); 85 86 udev_dev->dict = dict; 87 udev_dev->udev_ctx = udev_ref(udev_ctx); 88 89 return udev_dev; 90 } 91 92 struct udev_device * 93 udev_device_ref(struct udev_device *udev_device) 94 { 95 atomic_add_int(&udev_device->refs, 1); 96 97 return udev_device; 98 } 99 100 void 101 udev_device_unref(struct udev_device *udev_device) 102 { 103 int refcount; 104 105 refcount = atomic_fetchadd_int(&udev_device->refs, -1); 106 107 if (refcount == 1) { 108 atomic_subtract_int(&udev_device->refs, 0x400); /* in destruction */ 109 if (udev_device->dict != NULL) 110 prop_object_release(udev_device->dict); 111 112 udev_unref(udev_device->udev_ctx); 113 free(udev_device); 114 } 115 } 116 117 void 118 udev_device_set_action(struct udev_device *udev_device, int action) 119 { 120 udev_device->ev_type = action; 121 } 122 123 const char * 124 udev_device_get_action(struct udev_device *udev_device) 125 { 126 const char *action; 127 128 switch (udev_device->ev_type) { 129 case UDEV_EVENT_ATTACH: 130 action = "add"; 131 break; 132 133 case UDEV_EVENT_DETACH: 134 action = "remove"; 135 break; 136 137 default: 138 action = "none"; 139 break; 140 } 141 142 return action; 143 } 144 145 dev_t 146 udev_device_get_devnum(struct udev_device *udev_device) 147 { 148 prop_number_t pn; 149 dev_t devnum; 150 151 if (udev_device->dict == NULL) 152 return 0; 153 154 pn = prop_dictionary_get(udev_device->dict, "devnum"); 155 if (pn == NULL) 156 return 0; 157 158 devnum = prop_number_unsigned_integer_value(pn); 159 160 return devnum; 161 } 162 163 uint64_t 164 udev_device_get_kptr(struct udev_device *udev_device) 165 { 166 prop_number_t pn; 167 uint64_t kptr; 168 169 if (udev_device->dict == NULL) 170 return 0; 171 172 pn = prop_dictionary_get(udev_device->dict, "kptr"); 173 if (pn == NULL) 174 return 0; 175 176 kptr = prop_number_unsigned_integer_value(pn); 177 178 return kptr; 179 } 180 181 int32_t 182 udev_device_get_major(struct udev_device *udev_device) 183 { 184 prop_number_t pn; 185 int32_t major; 186 187 if (udev_device->dict == NULL) 188 return 0; 189 190 pn = prop_dictionary_get(udev_device->dict, "major"); 191 if (pn == NULL) 192 return 0; 193 194 major = (int32_t)prop_number_integer_value(pn); 195 196 return major; 197 } 198 199 int32_t 200 udev_device_get_minor(struct udev_device *udev_device) 201 { 202 prop_number_t pn; 203 int32_t minor; 204 205 if (udev_device->dict == NULL) 206 return 0; 207 208 pn = prop_dictionary_get(udev_device->dict, "minor"); 209 if (pn == NULL) 210 return 0; 211 212 minor = (int32_t)prop_number_integer_value(pn); 213 214 return minor; 215 } 216 217 const char * 218 udev_device_get_devnode(struct udev_device *udev_device) 219 { 220 dev_t devnum; 221 222 devnum = udev_device_get_devnum(udev_device); 223 if (devnum == 0) 224 return 0; 225 226 return devname(devnum, S_IFCHR); 227 } 228 229 const char * 230 udev_device_get_property_value(struct udev_device *udev_device, 231 const char *key) 232 { 233 prop_object_t po; 234 prop_number_t pn; 235 prop_string_t ps; 236 static char buf[128]; /* XXX: might cause trouble */ 237 const char *str = NULL; 238 239 if (udev_device->dict == NULL) 240 return NULL; 241 242 if ((po = prop_dictionary_get(udev_device->dict, key)) == NULL) 243 return NULL; 244 245 if (prop_object_type(po) == PROP_TYPE_STRING) { 246 ps = po; 247 str = __DECONST(char *, prop_string_cstring_nocopy(ps)); 248 } else if (prop_object_type(po) == PROP_TYPE_NUMBER) { 249 pn = po; 250 if (prop_number_unsigned(pn)) { 251 snprintf(buf, sizeof(buf), "%" PRIu64, 252 prop_number_unsigned_integer_value(pn)); 253 } else { 254 snprintf(buf, sizeof(buf), "%" PRIi64, 255 prop_number_integer_value(pn)); 256 } 257 str = buf; 258 } 259 return str; 260 } 261 262 const char * 263 udev_device_get_subsystem(struct udev_device *udev_device) 264 { 265 return udev_device_get_property_value(udev_device, "subsystem"); 266 } 267 268 const char * 269 udev_device_get_driver(struct udev_device *udev_device) 270 { 271 return udev_device_get_property_value(udev_device, "driver"); 272 } 273 274 prop_dictionary_t 275 udev_device_get_dictionary(struct udev_device *udev_device) 276 { 277 return udev_device->dict; 278 } 279 280 struct udev * 281 udev_device_get_udev(struct udev_device *udev_device) 282 { 283 return udev_device->udev_ctx; 284 } 285 286