chiark / gitweb /
[PATCH] replace strncpy()/strncat() by strlcpy()/strlcat()
[elogind.git] / udev_add.c
1 /*
2  * udev-add.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7  *
8  *
9  *      This program is free software; you can redistribute it and/or modify it
10  *      under the terms of the GNU General Public License as published by the
11  *      Free Software Foundation version 2 of the License.
12  * 
13  *      This program is distributed in the hope that it will be useful, but
14  *      WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *      General Public License for more details.
17  * 
18  *      You should have received a copy of the GNU General Public License along
19  *      with this program; if not, write to the Free Software Foundation, Inc.,
20  *      675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <grp.h>
33 #include <net/if.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36 #include <linux/sockios.h>
37
38 #include "libsysfs/sysfs/libsysfs.h"
39 #include "udev_libc_wrapper.h"
40 #include "udev.h"
41 #include "udev_utils.h"
42 #include "udev_sysfs.h"
43 #include "udev_version.h"
44 #include "logging.h"
45 #include "namedev.h"
46 #include "udev_db.h"
47 #include "udev_selinux.h"
48
49
50 int udev_make_node(struct udevice *udev, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
51 {
52         struct stat stats;
53         int retval = 0;
54
55         if (stat(file, &stats) != 0)
56                 goto create;
57
58         /* preserve node with already correct numbers, to not change the inode number */
59         if (((stats.st_mode & S_IFMT) == S_IFBLK || (stats.st_mode & S_IFMT) == S_IFCHR) &&
60             (stats.st_rdev == devt)) {
61                 dbg("preserve file '%s', cause it has correct dev_t", file);
62                 selinux_setfilecon(file, udev->kernel_name, stats.st_mode);
63                 goto perms;
64         }
65
66         if (unlink(file) != 0)
67                 dbg("unlink(%s) failed with error '%s'", file, strerror(errno));
68         else
69                 dbg("already present file '%s' unlinked", file);
70
71 create:
72         switch (udev->type) {
73         case BLOCK:
74                 mode |= S_IFBLK;
75                 break;
76         case CLASS:
77                 mode |= S_IFCHR;
78                 break;
79         default:
80                 dbg("unknown node type %c\n", udev->type);
81                 return -EINVAL;
82         }
83
84         selinux_setfscreatecon(file, udev->kernel_name, mode);
85         retval = mknod(file, mode, devt);
86         if (retval != 0) {
87                 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
88                     file, mode, major(devt), minor(devt), strerror(errno));
89                 goto exit;
90         }
91
92 perms:
93         dbg("chmod(%s, %#o)", file, mode);
94         if (chmod(file, mode) != 0) {
95                 dbg("chmod(%s, %#o) failed with error '%s'", file, mode, strerror(errno));
96                 goto exit;
97         }
98
99         if (uid != 0 || gid != 0) {
100                 dbg("chown(%s, %u, %u)", file, uid, gid);
101                 if (chown(file, uid, gid) != 0) {
102                         dbg("chown(%s, %u, %u) failed with error '%s'",
103                             file, uid, gid, strerror(errno));
104                         goto exit;
105                 }
106         }
107
108 exit:
109         return retval;
110 }
111
112 static int create_node(struct udevice *udev, struct sysfs_class_device *class_dev)
113 {
114         char filename[PATH_SIZE];
115         char partitionname[PATH_SIZE];
116         struct name_entry *name_loop;
117         uid_t uid = 0;
118         gid_t gid = 0;
119         int tail;
120         int i;
121
122         snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
123         filename[sizeof(filename)-1] = '\0';
124
125         /* create parent directories if needed */
126         if (strchr(udev->name, '/'))
127                 create_path(filename);
128
129         if (udev->owner[0] != '\0') {
130                 char *endptr;
131                 unsigned long id = strtoul(udev->owner, &endptr, 10);
132
133                 if (endptr[0] == '\0')
134                         uid = (uid_t) id;
135                 else
136                         uid = lookup_user(udev->owner);
137         }
138
139         if (udev->group[0] != '\0') {
140                 char *endptr;
141                 unsigned long id = strtoul(udev->group, &endptr, 10);
142
143                 if (endptr[0] == '\0')
144                         gid = (gid_t) id;
145                 else
146                         gid = lookup_group(udev->group);
147         }
148
149         if (!udev->test_run) {
150                 info("creating device node '%s'", filename);
151                 if (udev_make_node(udev, filename, udev->devt, udev->mode, uid, gid) != 0)
152                         goto error;
153         } else {
154                 info("creating device node '%s', major = '%d', minor = '%d', "
155                      "mode = '%#o', uid = '%d', gid = '%d'", filename,
156                      major(udev->devt), minor(udev->devt), udev->mode, uid, gid);
157         }
158
159         /* create all_partitions if requested */
160         if (udev->partitions) {
161                 struct sysfs_attribute *attr;
162                 int range;
163
164                 /* take the maximum registered minor range */
165                 attr = sysfs_get_classdev_attr(class_dev, "range");
166                 if (attr) {
167                         range = atoi(attr->value);
168                         if (range > 1)
169                                 udev->partitions = range-1;
170                 }
171                 info("creating device partition nodes '%s[1-%i]'", filename, udev->partitions);
172                 if (!udev->test_run) {
173                         for (i = 1; i <= udev->partitions; i++) {
174                                 dev_t part_devt;
175
176                                 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
177                                 partitionname[sizeof(partitionname)-1] = '\0';
178                                 part_devt = makedev(major(udev->devt), minor(udev->devt)+1);
179                                 udev_make_node(udev, partitionname, part_devt, udev->mode, uid, gid);
180                         }
181                 }
182         }
183
184         /* create symlink(s) if requested */
185         list_for_each_entry(name_loop, &udev->symlink_list, node) {
186                 char linktarget[PATH_SIZE];
187
188                 snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
189                 filename[sizeof(filename)-1] = '\0';
190
191                 dbg("symlink '%s' to node '%s' requested", filename, udev->name);
192                 if (!udev->test_run)
193                         if (strchr(filename, '/'))
194                                 create_path(filename);
195
196                 /* optimize relative link */
197                 linktarget[0] = '\0';
198                 i = 0;
199                 tail = 0;
200                 while (udev->name[i] && (udev->name[i] == name_loop->name[i])) {
201                         if (udev->name[i] == '/')
202                                 tail = i+1;
203                         i++;
204                 }
205                 while (name_loop->name[i] != '\0') {
206                         if (name_loop->name[i] == '/')
207                                 strlcat(linktarget, "../", sizeof(linktarget));
208                         i++;
209                 }
210
211                 strlcat(linktarget, &udev->name[tail], sizeof(linktarget));
212
213                 dbg("symlink(%s, %s)", linktarget, filename);
214                 if (!udev->test_run) {
215                         selinux_setfscreatecon(filename, udev->kernel_name, S_IFLNK);
216                         unlink(filename);
217                         if (symlink(linktarget, filename) != 0)
218                                 dbg("symlink(%s, %s) failed with error '%s'",
219                                     linktarget, filename, strerror(errno));
220                 }
221         }
222
223         return 0;
224 error:
225         return -1;
226 }
227
228 static int rename_net_if(struct udevice *udev)
229 {
230         int sk;
231         struct ifreq ifr;
232         int retval;
233
234         dbg("changing net interface name from '%s' to '%s'", udev->kernel_name, udev->name);
235         if (udev->test_run)
236                 return 0;
237
238         sk = socket(PF_INET, SOCK_DGRAM, 0);
239         if (sk < 0) {
240                 dbg("error opening socket");
241                 return -1;
242         }
243
244         memset(&ifr, 0x00, sizeof(struct ifreq));
245         strlcpy(ifr.ifr_name, udev->kernel_name, IFNAMSIZ);
246         strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ);
247
248         retval = ioctl(sk, SIOCSIFNAME, &ifr);
249         if (retval != 0)
250                 dbg("error changing net interface name");
251         close(sk);
252
253         return retval;
254 }
255
256 int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev)
257 {
258         char *pos;
259         int retval = 0;
260
261         if (udev->type == BLOCK || udev->type == CLASS) {
262                 udev->devt = get_devt(class_dev);
263                 if (!udev->devt) {
264                         dbg("no dev-file found, do nothing");
265                         return 0;
266                 }
267         }
268
269         if (namedev_name_device(udev, class_dev) != 0)
270                 return 0;
271
272         dbg("adding name='%s'", udev->name);
273
274         selinux_init();
275
276         if (udev->type == BLOCK || udev->type == CLASS) {
277                 retval = create_node(udev, class_dev);
278                 if (retval != 0)
279                         goto exit;
280
281                 if (udev_db_add_device(udev) != 0)
282                         dbg("udev_db_add_dev failed, but we create the node anyway, "
283                             "remove might not work for custom names");
284
285                 /* use full path to the environment */
286                 snprintf(udev->devname, sizeof(udev->devname), "%s/%s", udev_root, udev->name);
287                 udev->devname[sizeof(udev->devname)-1] = '\0';
288
289         } else if (udev->type == NET) {
290                 /* look if we want to change the name of the netif */
291                 if (strcmp(udev->name, udev->kernel_name) != 0) {
292                         retval = rename_net_if(udev);
293                         if (retval != 0)
294                                 goto exit;
295
296                         /* we've changed the name, now fake the devpath, cause the
297                          * original kernel name sleeps with the fishes and we don't
298                          * get an event from the kernel with the new name
299                          */
300                         pos = strrchr(udev->devpath, '/');
301                         if (pos != NULL) {
302                                 pos[1] = '\0';
303                                 strlcat(udev->devpath, udev->name, sizeof(udev->devpath));
304                                 setenv("DEVPATH", udev->devpath, 1);
305                                 setenv("INTERFACE", udev->name, 1);
306                         }
307
308                         /* use netif name for the environment */
309                         strlcpy(udev->devname, udev->name, sizeof(udev->devname));
310                 }
311         }
312
313 exit:
314         selinux_restore();
315
316         return retval;
317 }