chiark / gitweb /
udev_device_get_parent_with_subsystem_devtype(): Clarify documentation
[elogind.git] / libudev / libudev-device-private.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  */
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <stddef.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <string.h>
19 #include <sys/stat.h>
20
21 #include "libudev.h"
22 #include "libudev-private.h"
23
24 int udev_device_update_db(struct udev_device *udev_device)
25 {
26         struct udev *udev = udev_device_get_udev(udev_device);
27         char filename[UTIL_PATH_SIZE];
28         FILE *f;
29         char target[232]; /* on 64bit, tmpfs inlines up to 239 bytes */
30         size_t devlen = strlen(udev_get_dev_path(udev))+1;
31         char *s;
32         size_t l;
33         struct udev_list_entry *list_entry;
34         int ret;
35
36         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/",
37                       udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
38         unlink(filename);
39
40         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
41                 if (udev_list_entry_get_flag(list_entry))
42                         goto file;
43         if (udev_device_get_num_fake_partitions(udev_device) != 0)
44                 goto file;
45         if (udev_device_get_ignore_remove(udev_device))
46                 goto file;
47         if (udev_device_get_devlink_priority(udev_device) != 0)
48                 goto file;
49         if (udev_device_get_event_timeout(udev_device) >= 0)
50                 goto file;
51         if (udev_device_get_watch_handle(udev_device) >= 0)
52                 goto file;
53         if (udev_device_get_devnode(udev_device) == NULL)
54                 goto out;
55
56         /*
57          * if we have only the node and symlinks to store, try not to waste
58          * tmpfs memory -- store values, if they fit, in a symlink target
59          */
60         s = target;
61         l = util_strpcpy(&s, sizeof(target), &udev_device_get_devnode(udev_device)[devlen]);
62         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) {
63                 l = util_strpcpyl(&s, l, " ", &udev_list_entry_get_name(list_entry)[devlen], NULL);
64                 if (l == 0) {
65                         info(udev, "size of links too large, create file\n");
66                         goto file;
67                 }
68         }
69         info(udev, "create db link (%s)\n", target);
70         udev_selinux_setfscreatecon(udev, filename, S_IFLNK);
71         util_create_path(udev, filename);
72         ret = symlink(target, filename);
73         udev_selinux_resetfscreatecon(udev);
74         if (ret == 0)
75                 goto out;
76 file:
77         util_create_path(udev, filename);
78         f = fopen(filename, "w");
79         if (f == NULL) {
80                 err(udev, "unable to create db file '%s': %m\n", filename);
81                 return -1;
82                 }
83         info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
84
85         if (udev_device_get_devnode(udev_device) != NULL) {
86                 fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
87                 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
88                         fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]);
89         }
90         if (udev_device_get_devlink_priority(udev_device) != 0)
91                 fprintf(f, "L:%i\n", udev_device_get_devlink_priority(udev_device));
92         if (udev_device_get_event_timeout(udev_device) >= 0)
93                 fprintf(f, "T:%i\n", udev_device_get_event_timeout(udev_device));
94         if (udev_device_get_num_fake_partitions(udev_device) != 0)
95                 fprintf(f, "A:%i\n", udev_device_get_num_fake_partitions(udev_device));
96         if (udev_device_get_ignore_remove(udev_device))
97                 fprintf(f, "R:%i\n", udev_device_get_ignore_remove(udev_device));
98         if (udev_device_get_watch_handle(udev_device) >= 0)
99                 fprintf(f, "W:%i\n", udev_device_get_watch_handle(udev_device));
100         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
101                 if (!udev_list_entry_get_flag(list_entry))
102                         continue;
103                 fprintf(f, "E:%s=%s\n",
104                         udev_list_entry_get_name(list_entry),
105                         udev_list_entry_get_value(list_entry));
106         }
107         fclose(f);
108 out:
109         return 0;
110 }
111
112 int udev_device_delete_db(struct udev_device *udev_device)
113 {
114         struct udev *udev = udev_device_get_udev(udev_device);
115         char filename[UTIL_PATH_SIZE];
116
117         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/",
118                       udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
119         unlink(filename);
120         return 0;
121 }
122
123 int udev_device_rename_db(struct udev_device *udev_device)
124 {
125         struct udev *udev = udev_device_get_udev(udev_device);
126         char filename_old[UTIL_PATH_SIZE];
127         char filename[UTIL_PATH_SIZE];
128
129         util_strscpyl(filename_old, sizeof(filename_old), udev_get_dev_path(udev), "/.udev/db/",
130                       udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname_old(udev_device), NULL);
131         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/",
132                       udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
133         return rename(filename_old, filename);
134 }