chiark / gitweb /
remove "ignore_remove" option
[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_flags(list_entry))
43                         goto file;
44         if (udev_device_get_devlink_priority(udev_device) != 0)
45                 goto file;
46         if (udev_device_get_event_timeout(udev_device) >= 0)
47                 goto file;
48         if (udev_device_get_watch_handle(udev_device) >= 0)
49                 goto file;
50         if (udev_device_get_devnode(udev_device) == NULL)
51                 goto out;
52
53         /*
54          * if we have only the node and symlinks to store, try not to waste
55          * tmpfs memory -- store values, if they fit, in a symlink target
56          */
57         s = target;
58         l = util_strpcpy(&s, sizeof(target), &udev_device_get_devnode(udev_device)[devlen]);
59         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) {
60                 l = util_strpcpyl(&s, l, " ", &udev_list_entry_get_name(list_entry)[devlen], NULL);
61                 if (l == 0) {
62                         info(udev, "size of links too large, create file\n");
63                         goto file;
64                 }
65         }
66         udev_selinux_setfscreatecon(udev, filename_tmp, S_IFLNK);
67         util_create_path(udev, filename_tmp);
68         ret = symlink(target, filename_tmp);
69         udev_selinux_resetfscreatecon(udev);
70         if (ret != 0)
71                 goto file;
72         ret = rename(filename_tmp, filename);
73         if (ret != 0)
74                 goto file;
75         info(udev, "created db link (%s)\n", target);
76         goto out;
77 file:
78         util_create_path(udev, filename_tmp);
79         f = fopen(filename_tmp, "w");
80         if (f == NULL) {
81                 err(udev, "unable to create temporary db file '%s': %m\n", filename_tmp);
82                 return -1;
83                 }
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_watch_handle(udev_device) >= 0)
95                 fprintf(f, "W:%i\n", udev_device_get_watch_handle(udev_device));
96         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
97                 if (!udev_list_entry_get_flags(list_entry))
98                         continue;
99                 fprintf(f, "E:%s=%s\n",
100                         udev_list_entry_get_name(list_entry),
101                         udev_list_entry_get_value(list_entry));
102         }
103         fclose(f);
104         rename(filename_tmp, filename);
105         info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
106 out:
107         return 0;
108 }
109
110 int udev_device_delete_db(struct udev_device *udev_device)
111 {
112         struct udev *udev = udev_device_get_udev(udev_device);
113         char filename[UTIL_PATH_SIZE];
114
115         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/",
116                       udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
117         unlink(filename);
118         return 0;
119 }
120
121 int udev_device_rename_db(struct udev_device *udev_device)
122 {
123         struct udev *udev = udev_device_get_udev(udev_device);
124         char filename_old[UTIL_PATH_SIZE];
125         char filename[UTIL_PATH_SIZE];
126
127         util_strscpyl(filename_old, sizeof(filename_old), udev_get_dev_path(udev), "/.udev/db/",
128                       udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname_old(udev_device), NULL);
129         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/",
130                       udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
131         return rename(filename_old, filename);
132 }