chiark / gitweb /
keymap: move from udev-extras
[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 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
25 {
26         char *s;
27         size_t l;
28
29         s = filename;
30         l = util_strpcpyl(&s, len, udev_get_dev_path(udev), "/.udev/db/", NULL);
31         return util_path_encode(devpath, s, l);
32 }
33
34 int udev_device_update_db(struct udev_device *udev_device)
35 {
36         struct udev *udev = udev_device_get_udev(udev_device);
37         char filename[UTIL_PATH_SIZE];
38         FILE *f;
39         char target[232]; /* on 64bit, tmpfs inlines up to 239 bytes */
40         size_t devlen = strlen(udev_get_dev_path(udev))+1;
41         char *s;
42         size_t l;
43         struct udev_list_entry *list_entry;
44         int ret;
45
46         devpath_to_db_path(udev, udev_device_get_devpath(udev_device), filename, sizeof(filename));
47         util_create_path(udev, filename);
48         unlink(filename);
49
50         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
51                 if (udev_list_entry_get_flag(list_entry))
52                         goto file;
53         if (udev_device_get_num_fake_partitions(udev_device) != 0)
54                 goto file;
55         if (udev_device_get_ignore_remove(udev_device))
56                 goto file;
57         if (udev_device_get_devlink_priority(udev_device) != 0)
58                 goto file;
59         if (udev_device_get_event_timeout(udev_device) >= 0)
60                 goto file;
61         if (udev_device_get_watch_handle(udev_device) >= 0)
62                 goto file;
63         if (udev_device_get_devnode(udev_device) == NULL)
64                 goto out;
65
66         /*
67          * if we have only the node and symlinks to store, try not to waste
68          * tmpfs memory -- store values, if they fit, in a symlink target
69          */
70         s = target;
71         l = util_strpcpy(&s, sizeof(target), &udev_device_get_devnode(udev_device)[devlen]);
72         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) {
73                 l = util_strpcpyl(&s, l, " ", &udev_list_entry_get_name(list_entry)[devlen], NULL);
74                 if (l == 0) {
75                         info(udev, "size of links too large, create file\n");
76                         goto file;
77                 }
78         }
79         info(udev, "create db link (%s)\n", target);
80         udev_selinux_setfscreatecon(udev, filename, S_IFLNK);
81         ret = symlink(target, filename);
82         udev_selinux_resetfscreatecon(udev);
83         if (ret == 0)
84                 goto out;
85 file:
86         f = fopen(filename, "w");
87         if (f == NULL) {
88                 err(udev, "unable to create db file '%s': %m\n", filename);
89                 return -1;
90                 }
91         info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
92
93         if (udev_device_get_devnode(udev_device) != NULL) {
94                 fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
95                 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
96                         fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]);
97         }
98         if (udev_device_get_devlink_priority(udev_device) != 0)
99                 fprintf(f, "L:%u\n", udev_device_get_devlink_priority(udev_device));
100         if (udev_device_get_event_timeout(udev_device) >= 0)
101                 fprintf(f, "T:%u\n", udev_device_get_event_timeout(udev_device));
102         if (udev_device_get_num_fake_partitions(udev_device) != 0)
103                 fprintf(f, "A:%u\n", udev_device_get_num_fake_partitions(udev_device));
104         if (udev_device_get_ignore_remove(udev_device))
105                 fprintf(f, "R:%u\n", udev_device_get_ignore_remove(udev_device));
106         if (udev_device_get_watch_handle(udev_device) >= 0)
107                 fprintf(f, "W:%u\n", udev_device_get_watch_handle(udev_device));
108         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
109                 if (!udev_list_entry_get_flag(list_entry))
110                         continue;
111                 fprintf(f, "E:%s=%s\n",
112                         udev_list_entry_get_name(list_entry),
113                         udev_list_entry_get_value(list_entry));
114         }
115         fclose(f);
116 out:
117         return 0;
118 }
119
120 int udev_device_delete_db(struct udev_device *udev_device)
121 {
122         struct udev *udev = udev_device_get_udev(udev_device);
123         char filename[UTIL_PATH_SIZE];
124
125         devpath_to_db_path(udev, udev_device_get_devpath(udev_device), filename, sizeof(filename));
126         unlink(filename);
127         return 0;
128 }
129
130 int udev_device_rename_db(struct udev_device *udev_device, const char *devpath_old)
131 {
132         struct udev *udev = udev_device_get_udev(udev_device);
133         char filename_old[UTIL_PATH_SIZE];
134         char filename[UTIL_PATH_SIZE];
135
136         devpath_to_db_path(udev, devpath_old, filename_old, sizeof(filename_old));
137         devpath_to_db_path(udev, udev_device_get_devpath(udev_device), filename, sizeof(filename));
138         return rename(filename_old, filename);
139 }