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