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