]>
git.ipfire.org Git - thirdparty/systemd.git/blob - udev.c
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"
36 static char *get_action(void)
40 action
= getenv("ACTION");
45 static char *get_device(void)
47 static char device
[255];
50 temp
= getenv("DEVPATH");
53 strcpy(device
, SYSFS_ROOT
);
60 * Right now the major/minor of a device is stored in a file called
62 * The number is stored as:
66 * The value is in hex.
67 * Yes, this will probably change when we go to a bigger major/minor
68 * range, and will have to be changed at that time.
70 static int get_major_minor (char *dev
, int *major
, int *minor
)
78 /* add the dev file to the directory and see if it's present */
79 strncpy(filename
, dev
, sizeof(filename
));
80 strncat(filename
, DEV_FILE
, sizeof(filename
));
81 fd
= open(filename
, O_RDONLY
);
83 dbg("Can't open %s", filename
);
87 /* get the major/minor */
88 retval
= read(fd
, line
, sizeof(line
));
90 dbg("read error on %s", dev
);
97 *major
= (int)strtol(&temp
[0], NULL
, 16);
102 *minor
= (int)strtol(&temp
[0], NULL
, 16);
104 dbg("found major = %d, minor = %d", *major
, *minor
);
113 * Here would go a call to the naming deamon, to get the name we want to have
114 * for this device. But for now, let's just default to whatever the kernel is
115 * calling the device as that will keep the "old-style" naming policy
117 static char *get_name(char *dev
, int major
, int minor
)
119 static char name
[100];
122 temp
= strrchr(dev
, '/');
125 strncpy(name
, &temp
[1], sizeof(name
));
127 dbg("name is %s", name
);
133 * Again, this will live in the naming deamon
135 static int get_mode(char *name
, char *dev
, int major
, int minor
)
137 /* just default everyone to rw for the world! */
142 * We also want to add some permissions here, and possibly some symlinks
144 static int create_node(char *name
, char type
, int major
, int minor
, int mode
)
147 char mode_string
[100];
149 char major_string
[20];
150 char minor_string
[20];
154 strncpy(filename
, UDEV_ROOT
, sizeof(filename
));
155 strncat(filename
, name
, sizeof(filename
));
157 snprintf(mode_string
, sizeof(mode_string
), "--mode=%#o", mode
);
158 snprintf(type_string
, sizeof(type_string
), "%c", type
);
159 snprintf(major_string
, sizeof(major_string
), "%d", major
);
160 snprintf(minor_string
, sizeof(minor_string
), "%d", minor
);
163 argv
[1] = mode_string
;
165 argv
[3] = type_string
;
166 argv
[4] = major_string
;
167 argv
[5] = minor_string
;
169 dbg ("executing %s %s %s %s %s %s",
170 argv
[0], argv
[1], argv
[2], argv
[3], argv
[4], argv
[5]);
173 /* we are the child, so lets run the program */
178 dbg ("fork failed.");
188 * We also want to clean up any symlinks that were created in create_node()
190 static int delete_node(char *name
)
194 strncpy(filename
, UDEV_ROOT
, sizeof(filename
));
195 strncat(filename
, name
, sizeof(filename
));
197 dbg("unlinking %s", filename
);
198 return unlink(filename
);
201 static int add_device(char *device
, char type
)
207 int retval
= -EINVAL
;
209 retval
= get_major_minor(device
, &major
, &minor
);
211 dbg ("get_major_minor failed");
215 name
= get_name(device
, major
, minor
);
217 dbg ("get_name failed");
222 mode
= get_mode(name
, device
, major
, minor
);
224 dbg ("get_mode failed");
229 return create_node(name
, type
, major
, minor
, mode
);
235 static int remove_device(char *device
)
240 name
= get_name(device
, 0, 0);
242 dbg ("get_name failed");
247 return delete_node(name
);
253 int main(int argc
, char *argv
[])
259 int retval
= -EINVAL
;
262 dbg ("unknown number of arguments");
268 /* sleep for a second or two to give the kernel a chance to
269 * create the dev file
273 /* for now, the block layer is the only place where block devices are */
275 if (strcmp(subsystem
, "block") == 0)
280 action
= get_action();
286 device
= get_device();
291 dbg("looking at %s", device
);
293 if (strcmp(action
, "add") == 0)
294 return add_device(device
, type
);
296 if (strcmp(action
, "remove") == 0)
297 return remove_device(device
);
299 dbg("Unknown action: %s", action
);