]>
git.ipfire.org Git - thirdparty/systemd.git/blob - udev.c
4bb41a869638edb48881351d51476113a89d3bec
6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
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.
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.
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.
32 #include "udev_version.h"
35 static char *get_action(void)
39 action
= getenv("ACTION");
44 static char *get_device(void)
46 static char device
[255];
49 temp
= getenv("DEVPATH");
52 strcpy(device
, SYSFS_ROOT
);
59 * Right now the major/minor of a device is stored in a file called
61 * The number is stored as:
65 * The value is in hex.
66 * Yes, this will probably change when we go to a bigger major/minor
67 * range, and will have to be changed at that time.
69 static int get_major_minor (char *dev
, int *major
, int *minor
)
77 /* add the dev file to the directory and see if it's present */
78 strncpy(filename
, dev
, sizeof(filename
));
79 strncat(filename
, DEV_FILE
, sizeof(filename
));
80 fd
= open(filename
, O_RDONLY
);
82 dbg("Can't open %s", filename
);
86 /* get the major/minor */
87 retval
= read(fd
, line
, sizeof(line
));
89 dbg("read error on %s", dev
);
96 *major
= (int)strtol(&temp
[0], NULL
, 16);
101 *minor
= (int)strtol(&temp
[0], NULL
, 16);
103 dbg("found major = %d, minor = %d", *major
, *minor
);
112 * Here would go a call to the naming deamon, to get the name we want to have
113 * for this device. But for now, let's just default to whatever the kernel is
114 * calling the device as that will keep the "old-style" naming policy
116 static char *get_name(char *dev
, int major
, int minor
)
118 static char name
[100];
121 temp
= strrchr(dev
, '/');
124 strncpy(name
, &temp
[1], sizeof(name
));
126 dbg("name is %s", name
);
132 * Again, this will live in the naming deamon
134 static int get_mode(char *name
, char *dev
, int major
, int minor
)
136 /* just default everyone to rw for the world! */
141 * We also want to add some permissions here, and possibly some symlinks
143 static int create_node(char *name
, char type
, int major
, int minor
, int mode
)
146 char mode_string
[100];
148 char major_string
[20];
149 char minor_string
[20];
153 strncpy(filename
, UDEV_ROOT
, sizeof(filename
));
154 strncat(filename
, name
, sizeof(filename
));
156 snprintf(mode_string
, sizeof(mode_string
), "--mode=%#o", mode
);
157 snprintf(type_string
, sizeof(type_string
), "%c", type
);
158 snprintf(major_string
, sizeof(major_string
), "%d", major
);
159 snprintf(minor_string
, sizeof(minor_string
), "%d", minor
);
162 argv
[1] = mode_string
;
164 argv
[3] = type_string
;
165 argv
[4] = major_string
;
166 argv
[5] = minor_string
;
168 dbg ("executing %s %s %s %s %s %s",
169 argv
[0], argv
[1], argv
[2], argv
[3], argv
[4], argv
[5]);
172 /* we are the child, so lets run the program */
177 dbg ("fork failed.");
186 static int add_node(char *device
, char type
)
192 int retval
= -EINVAL
;
194 retval
= get_major_minor(device
, &major
, &minor
);
196 dbg ("get_major_minor failed");
200 name
= get_name(device
, major
, minor
);
202 dbg ("get_name failed");
207 mode
= get_mode(name
, device
, major
, minor
);
209 dbg ("get_mode failed");
214 return create_node(name
, type
, major
, minor
, mode
);
220 static int remove_node(char *device
)
225 int main(int argc
, char *argv
[])
231 int retval
= -EINVAL
;
234 dbg ("unknown number of arguments");
238 /* for now, the block layer is the only place where block devices are */
240 if (strcmp(subsystem
, "block") == 0)
245 action
= get_action();
251 device
= get_device();
256 dbg("looking at %s", device
);
258 if (strcmp(action
, "add") == 0)
259 return add_node(device
, type
);
261 if (strcmp(action
, "remove") == 0)
262 return remove_node(device
);
264 dbg("Unknown action: %s", action
);