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