add common mkdir_dev() and mkdirs_dev() funcs

mkdir_dev() use for create subdir with mode 0755 in /dev directory
and return success if subdir already exist.
mkdirs_dev() used mkdir_dev() for create multiple subdirs.
This commit is contained in:
Led 2013-04-01 18:07:51 +03:00 committed by Michael Shigorin
parent 13019baef2
commit 1915f90346
5 changed files with 47 additions and 10 deletions

View File

@ -79,7 +79,7 @@ STAGE1_NETWORK_LIBS = $($(L)_STAGE1_NETWORK_LIBS)
STAGE1SRC = stage1.c log.c tools.c modules.c probing.c \
mount.c lomount.c automatic.c frontend-common.c \
cdrom.c disk.c \
cdrom.c disk.c common.c \
network.c dhcp.c url.c dns.c adsl.c
STAGE1OBJS = $(addprefix $(L)-,$(subst .c,.o,$(STAGE1SRC)))

32
common.c Normal file
View File

@ -0,0 +1,32 @@
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include "common.h"
int mkdir_dev(const char * name)
{
char pathname[PATH_MAX + 1] = "/dev/";
strcpy(pathname + 5, name);
return (mkdir(pathname, 0755) == 0 || errno == EEXIST) ? 0 : -1;
}
int mkdirs_dev(const char * name, ...)
{
va_list args;
int ret = 0;
va_start(args, name);
for (; name; name = va_arg(args, const char *))
if (mkdir_dev(name) && errno != EEXIST) {
ret = -1;
break;
}
va_end(args);
return ret;
}

7
common.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef _COMMON_H_
#define _COMMON_H_
int mkdir_dev(const char * name);
int mkdirs_dev(const char * name, ...);
#endif

10
init.c
View File

@ -36,6 +36,7 @@
#include "config-stage1.h"
#include "lomount.h"
#include "tools.h"
#include "common.h"
#define RAMFS_MAGIC 0x858458f6
#define TMPFS_MAGIC 0x01021994
@ -419,9 +420,7 @@ int main(int argc, char **argv)
if (setdomainname("", 0) < 0)
warn("could not set domainname");
if (mkdir("/dev/.initramfs", 0755) < 0 ||
mkdir("/dev/pts", 0755) < 0 ||
mkdir("/dev/shm", 0755) < 0)
if (mkdirs_dev(".initramfs", "pts", "shm", NULL) < 0)
fatal("mkdir\n");
if ((fd = open("/proc/sys/kernel/hotplug", O_WRONLY, 0)) < 0 ||
@ -433,8 +432,7 @@ int main(int argc, char **argv)
spawn_hook(init_top);
if (mkdir("/dev/.udev", 0755) < 0 ||
mkdir("/dev/.udev/db", 0755) < 0)
if (mkdirs_dev(".udev", ".udev/db", NULL) < 0)
fatal("/dev/.udev/db");
printf("Spawning udevd...");
@ -443,7 +441,7 @@ int main(int argc, char **argv)
usleep(500);
if (mkdir("/dev/.udev/queue", 0755) < 0 && errno != EEXIST)
if (mkdir_dev(".udev/queue") < 0)
fatal("cannot create /dev/.udev/queue");
if (waitpid(spawn(udevtrigger), &wait_status, 0) < 0 ||

View File

@ -39,6 +39,7 @@
#include "mount.h"
#include "dns.h"
#include "common.h"
#ifndef DISABLE_MEDIAS
@ -97,8 +98,7 @@ int ensure_dev_exists(char *dev)
ptr_begins_static_str(name, "cciss/")) {
/* Compaq Smart Array "ida/c0d0{p1}" */
ptr = strchr(name, '/');
mkdir("/dev/ida", 0755);
mkdir("/dev/cciss", 0755);
mkdirs_dev("ida", "cciss", NULL);
major = ptr_begins_static_str(name, "ida/") ? 72 : 104 + charstar_to_int(ptr+2);
ptr = strchr(ptr, 'd');
minor = 16 * charstar_to_int(ptr+1);
@ -106,7 +106,7 @@ int ensure_dev_exists(char *dev)
minor += charstar_to_int(ptr+1);
} else if (ptr_begins_static_str(name, "rd/")) {
/* DAC960 "rd/cXdXXpX" */
mkdir("/dev/rd", 0755);
mkdir_dev("rd");
major = 48 + charstar_to_int(name+4);
ptr = strchr(name+4, 'd');
minor = 8 * charstar_to_int(ptr+1);