libudev-device.c revision 37372bbc38d8542f482d90b26c90714429584421
c343be283b7152554bac0c02493a4e1759c163f7Kay Sievers * libudev - interface to udev device information
b3ae710c251d0ce5cf2cef63208e325497b5e323Zbigniew Jędrzejewski-Szmek * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers * This program is free software: you can redistribute it and/or modify
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers * it under the terms of the GNU General Public License as published by
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers * the Free Software Foundation, either version 2 of the License, or
afe3ab588a6b2992efe5a9b22ed038545ba3cdbfLennart Poettering * (at your option) any later version.
d19e85f0d474ed1882561b458d528cbae49f640eZbigniew Jędrzejewski-Szmek * This program is distributed in the hope that it will be useful,
d19e85f0d474ed1882561b458d528cbae49f640eZbigniew Jędrzejewski-Szmek * but WITHOUT ANY WARRANTY; without even the implied warranty of
d19e85f0d474ed1882561b458d528cbae49f640eZbigniew Jędrzejewski-Szmek * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d19e85f0d474ed1882561b458d528cbae49f640eZbigniew Jędrzejewski-Szmek * GNU General Public License for more details.
afe3ab588a6b2992efe5a9b22ed038545ba3cdbfLennart Poettering * You should have received a copy of the GNU General Public License
afe3ab588a6b2992efe5a9b22ed038545ba3cdbfLennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
c3a0d00d6dd6f5997d673e133ef6f9f856550b0aLennart Poettering long long int seqnum;
82e6c50c473f4be8df77c7a510577f1975eedddbLennart Poetteringstruct udev_device *device_init(struct udev *udev)
98cd2651988619bf606f0b27825440c4638a7e0bLennart Poettering udev_device = malloc(sizeof(struct udev_device));
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering memset(udev_device, 0x00, sizeof(struct udev_device));
bf27dcb613a3924b7f4ec74c2de29ace79d62b01Lennart Poettering info(udev_device->udev, "udev_device: %p created\n", udev_device);
2a441c8afe7c81f74cc2ceccdf9a71301cd39d4dLennart Poettering * udev_device_new_from_devpath:
40ff4a4abd76d71408e598ed02d7860b8465fa9aLennart Poettering * @udev: udev library context
40ff4a4abd76d71408e598ed02d7860b8465fa9aLennart Poettering * @devpath: sys device path
40ff4a4abd76d71408e598ed02d7860b8465fa9aLennart Poettering * Create new udev device, and fill in information from the sysfs
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poettering * device and the udev database entry. The devpath must not contain
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poettering * the sysfs mount path, and must contain a leading '/'.
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poettering * The initial refcount is 1, and needs to be decremented to
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poettering * release the ressources of the udev device.
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poettering * Returns: a new udev device, or #NULL, if it does not exist
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poetteringstruct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath)
8514b67754c5ff7fa628929b3d27131010c21842Lennart Poettering strlcpy(path, udev_get_sys_path(udev), sizeof(path));
f81e67f79fa856aa2ecffad4d014772ce981745cLennart Poettering /* resolve possible symlink to real path */
d48b7bd271b1e70924c8485d2f95c2f5a1ae77cbLennart Poettering sysfs_resolve_link(udev, path, sizeof(path));
d48b7bd271b1e70924c8485d2f95c2f5a1ae77cbLennart Poettering device_set_devpath(udev_device, devpath);
25e14499c4c5b02229d05a5bc26c3693ade5f987Lennart Poettering info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
821cc13ddae40fb7608458b44aaa7a3fd33d56d9Lennart Poettering info(udev, "device %p filled with udev database data\n", udev_device);
8483d73ff158ee0d51ccbba09a470cc6ae9b071aLennart Poettering asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev), udevice->name);
8483d73ff158ee0d51ccbba09a470cc6ae9b071aLennart Poettering list_for_each_entry(name_loop, &udevice->symlink_list, node) {
8483d73ff158ee0d51ccbba09a470cc6ae9b071aLennart Poettering strlcpy(name, udev_get_dev_path(udev), sizeof(name));
8483d73ff158ee0d51ccbba09a470cc6ae9b071aLennart Poettering strlcat(name, name_loop->name, sizeof(name));
25e773eeb4f853804e1bf0dbd9a184f23e9b2a97Kay Sievers name_list_add(udev, &udev_device->link_list, name, 0);
6dab5bb18151c80fc39bd51f03dcff40b920de3eLennart Poettering list_for_each_entry(name_loop, &udevice->env_list, node)
384a4be2b00cb95ce215dd343cc9aa77adc9b1ecLennart Poettering name_list_add(udev_device->udev, &udev_device->env_list, name_loop->name, 0);
533bb267f13e2f7b4d7b78de30e821dc81c82335Lennart Poettering * udev_device_get_udev:
279f036675536d55c901562b49f9df146af1a0e3Lennart Poettering * @udev_device: udev device
b42de08aa4c97636e42c28c7bce08f0d7c2a719aZbigniew Jędrzejewski-Szmek * Retrieve the udev library context the device was created with.
b42de08aa4c97636e42c28c7bce08f0d7c2a719aZbigniew Jędrzejewski-Szmek * Returns: the udev library context
202aea456dfb279cd34da7bfd1880f6ac0fd849fLennart Poetteringstruct udev *udev_device_get_udev(struct udev_device *udev_device)
14a32924c9b46817c92ae11c1147a59dcb62012bLennart Poettering * udev_device_ref:
14a32924c9b46817c92ae11c1147a59dcb62012bLennart Poettering * @udev_device: udev device
563b1bdc09efe0cf94dd3f514f30376ca854c1ceLennart Poettering * Take a reference of a udev device.
5dcf983854c2e8314dbee239180255490ec8ae1cLennart Poettering * Returns: the passed udev device
5dcf983854c2e8314dbee239180255490ec8ae1cLennart Poetteringstruct udev_device *udev_device_ref(struct udev_device *udev_device)
41488fe9024a8955d19811620fd55dcc56a5b2baLennart Poettering * udev_device_unref:
41488fe9024a8955d19811620fd55dcc56a5b2baLennart Poettering * @udev_device: udev device
41488fe9024a8955d19811620fd55dcc56a5b2baLennart Poettering * Drop a reference of a udev device. If the refcount reaches zero,
ca70bec9261977336c94f44d5fcf37e1c495326aLennart Poettering * the ressources of the device will be released.
1dbe0a6efda7b1d35957eab7e1d56a2c69d806d9Lennart Poetteringvoid udev_device_unref(struct udev_device *udev_device)
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek name_list_cleanup(udev_device->udev, &udev_device->link_list);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek name_list_cleanup(udev_device->udev, &udev_device->env_list);
23c4091dc2b85d117512e89233fdeb47d1ff3d92Lennart Poettering info(udev_device->udev, "udev_device: %p released\n", udev_device);
bc07548926ec5ed7b13df8d3656654f238e0b9a7Lennart Poettering * udev_device_get_devpath:
bc07548926ec5ed7b13df8d3656654f238e0b9a7Lennart Poettering * @udev_device: udev device
279f036675536d55c901562b49f9df146af1a0e3Lennart Poettering * Retrieve the kernel devpath value of the udev device. The path
279f036675536d55c901562b49f9df146af1a0e3Lennart Poettering * does not contain the sys mount point, and starts with a '/'.
e2a69298819b58f008be61d314f8ab95ccaec427Lennart Poettering * Returns: the devpath of the udev device
2834ffe78d7fd8be118429aa1449ac72641638c2Lennart Poetteringconst char *udev_device_get_devpath(struct udev_device *udev_device)
3de03738fc970496d2d3da668c72767a48ccc41bLennart Poettering * udev_device_get_syspath:
3de03738fc970496d2d3da668c72767a48ccc41bLennart Poettering * @udev_device: udev device
2b1c3767515672dfd0f5e0a9c9d7ac3a16a6a361Lennart Poettering * Retrieve the sys path of the udev device. The path is an
37efac5ddb21fd91ed420c070ed07f375e78b3b9Lennart Poettering * absolute path and starts with the sys mount point.
7348b3adb324614132cf376f478e883bd7de28f1Lennart Poettering * Returns: the sys path of the udev device
e107ed185ef08945102834234a05ec51bb438685Lennart Poetteringconst char *udev_device_get_syspath(struct udev_device *udev_device)
01083ad094664e5c685060f4fb35a05ea2f212edLennart Poettering * udev_device_get_devname:
edb2935c5c5b95c42b8679086f60da5eafad74cbLennart Poettering * @udev_device: udev device
769918ecd30c0f7ee6e87b9aa6226d956bd2f530Lennart Poettering * Retrieve the device node file name belonging to the udev device.
769918ecd30c0f7ee6e87b9aa6226d956bd2f530Lennart Poettering * The path is an absolute path, and starts with the device directory.
769918ecd30c0f7ee6e87b9aa6226d956bd2f530Lennart Poettering * Returns: the device node file name of the udev device, or #NULL if no device node exists
2a781fc9bd33982c81e5ff75974a442a33d4f167Lennart Poetteringconst char *udev_device_get_devname(struct udev_device *udev_device)
eda8f06755bd98c4639293c26b856c225f0d1fe1Lennart Poettering * udev_device_get_subsystem:
eda8f06755bd98c4639293c26b856c225f0d1fe1Lennart Poettering * @udev_device: udev device
d4fdc205a4610965cee46408dbd046c922e7620cLennart Poettering * Retrieve the subsystem string of the udev device. The string does not
d4fdc205a4610965cee46408dbd046c922e7620cLennart Poettering * contain any "/".
af1082b04a3d45a9b1d796b4271f44e87e307026Lennart Poettering * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
af1082b04a3d45a9b1d796b4271f44e87e307026Lennart Poetteringconst char *udev_device_get_subsystem(struct udev_device *udev_device)
47c94a96df29080f8b3a97e7362df4e9c6ba3265Lennart Poettering if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) < 2)
09ecd746c9d6581664873674c2188f8c93ed7780Lennart Poettering udev_device->subsystem = strdup(subsystem);
09ecd746c9d6581664873674c2188f8c93ed7780Lennart Poettering * udev_device_get_devlinks:
ef417cfd2211ae017a38b9796c6db29130133e63Zbigniew Jędrzejewski-Szmek * @udev_device: udev device
ef417cfd2211ae017a38b9796c6db29130133e63Zbigniew Jędrzejewski-Szmek * @cb: function to be called for every device link found
3333d748facc15f49935b6b793490ba0824976e6Zbigniew Jędrzejewski-Szmek * @data: data to be passed to the function
f38afcd0c7f558ca5bf0854b42f8c6954f8ad7f3Lennart Poettering * Retrieve the device links pointing to the device file of the
f38afcd0c7f558ca5bf0854b42f8c6954f8ad7f3Lennart Poettering * udev device. For every device link, the passed function will be
f38afcd0c7f558ca5bf0854b42f8c6954f8ad7f3Lennart Poettering * called with the device link string.
f38afcd0c7f558ca5bf0854b42f8c6954f8ad7f3Lennart Poettering * The path is an absolute path, and starts with the device directory.
f38afcd0c7f558ca5bf0854b42f8c6954f8ad7f3Lennart Poettering * If the function returns 1, remaning device links will be ignored.
f38afcd0c7f558ca5bf0854b42f8c6954f8ad7f3Lennart Poettering * Returns: the number of device links passed to the caller, or a negative value on error
f38afcd0c7f558ca5bf0854b42f8c6954f8ad7f3Lennart Poetteringint udev_device_get_devlinks(struct udev_device *udev_device,
f38afcd0c7f558ca5bf0854b42f8c6954f8ad7f3Lennart Poettering int (*cb)(struct udev_device *udev_device, const char *value, void *data),
f38afcd0c7f558ca5bf0854b42f8c6954f8ad7f3Lennart Poettering list_for_each_entry(name_loop, &udev_device->link_list, node) {
718db96199eb307751264e4163555662c9a389faLennart Poettering if (cb(udev_device, name_loop->name, data) != 0)
416446221d905b6815175dc4d525d27f8ae43d1bLennart Poettering * udev_device_get_properties:
7f79cd7109a60810140a045cc725291fc5515264Lennart Poettering * @udev_device: udev device
0aafd43d235982510d1c40564079f7bcec0c7c19Lennart Poettering * @cb: function to be called for every property found
19aadacf92ad86967ffb678e37b2ff9e83cb9480Jan Engelhardt * @data: data to be passed to the function
df5f6971e6e15b4632884916c71daa076c8bae96Lennart Poettering * Retrieve the property key/value pairs belonging to the
df5f6971e6e15b4632884916c71daa076c8bae96Lennart Poettering * udev device. For every key/value pair, the passed function will be
fcba531ed4c6e6f8f21d8ca4e3a56e3162b1c578Lennart Poettering * called. If the function returns 1, remaning properties will be
c3bb87dbab8b79bb9253407cb5b7f3e6fe8db395Lennart Poettering * Returns: the number of properties passed to the caller, or a negative value on error
18d4e7c26e7806ac363d19989df7144d5058ce41Lennart Poetteringint udev_device_get_properties(struct udev_device *udev_device,
bdeeb6b543a2a2d0a494f17b85f1498859cdfc2fLennart Poettering int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
d0928791499734e202460d5c027b5d3e0d28e7abLennart Poettering list_for_each_entry(name_loop, &udev_device->env_list, node) {
e8bbb7c7e9163420cb1ba92f82dbc09aab577d1aLennart Poettering strncpy(name, name_loop->name, PATH_SIZE);
ab9716c2489f9141ed13ec22dbb216b3e6fbd6b5Lennart Poetteringconst char *udev_device_get_driver(struct udev_device *udev_device)
69727e6dc69ae5d9b5ae3681723778a3faa354e9Lennart Poetteringdev_t udev_device_get_devnum(struct udev_device *udev_device)
f38afcd0c7f558ca5bf0854b42f8c6954f8ad7f3Lennart Poetteringconst char *udev_device_get_action(struct udev_device *udev_device)
154ff088d371bee5651eaa2bc9bde8a34c185656Lennart Poetteringunsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
ab8e074ce25b9947314c69e17afe1bd2527ee26dLennart Poetteringint device_set_devpath(struct udev_device *udev_device, const char *devpath)
ab8e074ce25b9947314c69e17afe1bd2527ee26dLennart Poettering if (asprintf(&udev_device->syspath, "%s%s", udev_get_sys_path(udev_device->udev), devpath) < 0)
b454b11220e87add6d0f011695c7912b009c853dLennart Poettering udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
fff87a35d9e26c0d4ea41273a963c0eb20e18da4Zbigniew Jędrzejewski-Szmekint device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
fff87a35d9e26c0d4ea41273a963c0eb20e18da4Zbigniew Jędrzejewski-Szmek udev_device->subsystem = strdup(subsystem);
b5c03638d48c07aa0eaf13b5f54000c7133e1883Lennart Poetteringint device_set_devname(struct udev_device *udev_device, const char *devname)
09f727eebd87661f263d3c2c1e0de7b7771acd40Lennart Poetteringint device_add_devlink(struct udev_device *udev_device, const char *devlink)
c144692179098c1861f2aeafc67689a74439cf4cLennart Poettering if (name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
0be8342c04bbf129b4a21e5073eacccbbce4e896Lennart Poetteringint device_add_property(struct udev_device *udev_device, const char *property)
0be8342c04bbf129b4a21e5073eacccbbce4e896Lennart Poettering if (name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
826872b61e4857dfffe63ba84e2b005623baecd6Lennart Poetteringint device_set_action(struct udev_device *udev_device, const char *action)
3679d1126bae52e02f6cd60fca196f616b9e660dLennart Poetteringint device_set_driver(struct udev_device *udev_device, const char *driver)
466784c8710e5cb0e0b86a16506d992d7ec5b619Kay Sieversconst char *device_get_devpath_old(struct udev_device *udev_device)
b44be3ecf6326c27aa2c6c6d1fe34e22e22592a0Lennart Poetteringint device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
b44be3ecf6326c27aa2c6c6d1fe34e22e22592a0Lennart Poettering udev_device->devpath_old = strdup(devpath_old);
b44be3ecf6326c27aa2c6c6d1fe34e22e22592a0Lennart Poetteringconst char *device_get_physdevpath(struct udev_device *udev_device)
f38afcd0c7f558ca5bf0854b42f8c6954f8ad7f3Lennart Poetteringint device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
f38afcd0c7f558ca5bf0854b42f8c6954f8ad7f3Lennart Poettering udev_device->physdevpath = strdup(physdevpath);
279f036675536d55c901562b49f9df146af1a0e3Lennart Poetteringint device_get_timeout(struct udev_device *udev_device)
279f036675536d55c901562b49f9df146af1a0e3Lennart Poetteringint device_set_timeout(struct udev_device *udev_device, int timeout)
e673ad0415d89c322e5b1a121e411f1b1d8075c0Lennart Poetteringint device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)