]>
Commit | Line | Data |
---|---|---|
8dfc8dbe | 1 | /* |
aa8734ff | 2 | * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org> |
8dfc8dbe | 3 | * |
55e9959b KS |
4 | * This program is free software: you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8dfc8dbe | 8 | * |
55e9959b KS |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
8dfc8dbe GKH |
16 | */ |
17 | ||
8e41d35d DS |
18 | #include <stdlib.h> |
19 | #include <stdio.h> | |
8ea84a8a | 20 | #include <string.h> |
c81b35c0 | 21 | #include <stddef.h> |
845d4751 | 22 | #include <unistd.h> |
8e41d35d DS |
23 | #include <fcntl.h> |
24 | #include <string.h> | |
34bb5d05 | 25 | #include <sys/stat.h> |
8e41d35d | 26 | |
6739707d | 27 | #include "udev.h" |
2b41e68a | 28 | |
7d563a17 | 29 | static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len) |
2b41e68a | 30 | { |
9c6ad9fb | 31 | size_t start; |
db481105 | 32 | |
e7e194a0 | 33 | /* translate to location of db file */ |
31c1f537 KS |
34 | util_strlcpy(filename, udev_get_dev_path(udev), len); |
35 | start = util_strlcat(filename, "/.udev/db/", len); | |
36 | util_strlcat(filename, devpath, len); | |
ecc9ec57 | 37 | return util_path_encode(&filename[start], len - start); |
2b41e68a | 38 | } |
ca999860 | 39 | |
aa8734ff | 40 | int udev_device_update_db(struct udev_device *udev_device) |
ca999860 | 41 | { |
aa8734ff | 42 | struct udev *udev = udev_device_get_udev(udev_device); |
17fcfb59 | 43 | char filename[UTIL_PATH_SIZE]; |
1e75cda3 | 44 | FILE *f; |
1e75cda3 | 45 | char target[232]; /* on 64bit, tmpfs inlines up to 239 bytes */ |
aa8734ff KS |
46 | size_t devlen = strlen(udev_get_dev_path(udev))+1; |
47 | struct udev_list_entry *list_entry; | |
1e75cda3 | 48 | int ret; |
ca999860 | 49 | |
aa8734ff KS |
50 | devpath_to_db_path(udev, |
51 | udev_device_get_devpath(udev_device), | |
52 | filename, sizeof(filename)); | |
54808d77 | 53 | util_create_path(udev, filename); |
6eee03ef | 54 | unlink(filename); |
554dde8e | 55 | |
aa8734ff KS |
56 | udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) |
57 | if (udev_list_entry_get_flag(list_entry)) | |
58 | goto file; | |
59 | if (udev_device_get_num_fake_partitions(udev_device)) | |
1e75cda3 | 60 | goto file; |
aa8734ff | 61 | if (udev_device_get_ignore_remove(udev_device)) |
1e75cda3 | 62 | goto file; |
aa8734ff KS |
63 | /* try not to waste tmpfs memory; store values, if they fit, in a symlink target */ |
64 | util_strlcpy(target, &udev_device_get_devnode(udev_device)[devlen], sizeof(target)); | |
65 | udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) { | |
1e75cda3 | 66 | size_t len; |
554dde8e | 67 | |
1e75cda3 | 68 | util_strlcat(target, " ", sizeof(target)); |
aa8734ff | 69 | len = util_strlcat(target, &udev_list_entry_get_name(list_entry)[devlen], sizeof(target)); |
1e75cda3 | 70 | if (len >= sizeof(target)) { |
aa8734ff | 71 | info(udev, "size of links too large, create file\n"); |
1e75cda3 | 72 | goto file; |
fbda4bab | 73 | } |
2b41e68a | 74 | } |
aa8734ff KS |
75 | info(udev, "create db link (%s)\n", target); |
76 | udev_selinux_setfscreatecon(udev, filename, S_IFLNK); | |
1e75cda3 | 77 | ret = symlink(target, filename); |
aa8734ff | 78 | udev_selinux_resetfscreatecon(udev); |
1e75cda3 KS |
79 | if (ret == 0) |
80 | goto out; | |
81 | file: | |
82 | f = fopen(filename, "w"); | |
83 | if (f == NULL) { | |
aa8734ff | 84 | err(udev, "unable to create db file '%s': %m\n", filename); |
1e75cda3 KS |
85 | return -1; |
86 | } | |
aa8734ff KS |
87 | info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename); |
88 | ||
89 | fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]); | |
90 | udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) | |
91 | fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]); | |
92 | if (udev_device_get_devlink_priority(udev_device) != 0) | |
93 | fprintf(f, "L:%u\n", udev_device_get_devlink_priority(udev_device)); | |
94 | if (udev_device_get_event_timeout(udev_device) >= 0) | |
95 | fprintf(f, "T:%u\n", udev_device_get_event_timeout(udev_device)); | |
96 | if (udev_device_get_num_fake_partitions(udev_device) != 0) | |
97 | fprintf(f, "A:%u\n", udev_device_get_num_fake_partitions(udev_device)); | |
98 | if (udev_device_get_ignore_remove(udev_device)) | |
99 | fprintf(f, "R:%u\n", udev_device_get_ignore_remove(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)); | |
1e75cda3 | 106 | } |
1e75cda3 KS |
107 | fclose(f); |
108 | out: | |
2b41e68a | 109 | return 0; |
ca999860 GKH |
110 | } |
111 | ||
aa8734ff | 112 | int udev_device_delete_db(struct udev_device *udev_device) |
8e41d35d | 113 | { |
17fcfb59 | 114 | char filename[UTIL_PATH_SIZE]; |
7a947ce5 | 115 | |
aa8734ff KS |
116 | devpath_to_db_path(udev_device_get_udev(udev_device), |
117 | udev_device_get_devpath(udev_device), | |
118 | filename, sizeof(filename)); | |
119 | unlink(filename); | |
a56ef382 | 120 | return 0; |
8e41d35d DS |
121 | } |
122 | ||
aa8734ff | 123 | int udev_device_rename_db(struct udev_device *udev_device, const char *devpath_old) |
8e41d35d | 124 | { |
aa8734ff | 125 | char filename_old[UTIL_PATH_SIZE]; |
17fcfb59 | 126 | char filename[UTIL_PATH_SIZE]; |
a9ce0a41 | 127 | |
aa8734ff KS |
128 | devpath_to_db_path(udev_device_get_udev(udev_device), |
129 | devpath_old, | |
130 | filename_old, sizeof(filename_old)); | |
131 | devpath_to_db_path(udev_device_get_udev(udev_device), | |
132 | udev_device_get_devpath(udev_device), | |
133 | filename, sizeof(filename)); | |
134 | return rename(filename_old, filename); | |
a9ce0a41 | 135 | } |