diff mbox

[LEDE-DEV,1/2] inittab: use more robust dev_exist() implementation

Message ID 8fd3a1a125a2321e0fe7a10b41f137e489d66378.1463499516.git.jo@mein.io
State Accepted
Headers show

Commit Message

Jo-Philipp Wich May 17, 2016, 3:42 p.m. UTC
Rework the dev_exist() function to use openat() in order to resolve the device
file relative to the "/dev" directory. Drop the now unused dev_open() function.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
---
 inittab.c | 31 ++++++++++++-------------------
 1 file changed, 12 insertions(+), 19 deletions(-)
diff mbox

Patch

diff --git a/inittab.c b/inittab.c
index 622601a..528396e 100644
--- a/inittab.c
+++ b/inittab.c
@@ -65,30 +65,23 @@  static char *ask = "/sbin/askfirst";
 
 static LIST_HEAD(actions);
 
-static int dev_open(const char *dev)
+static int dev_exist(const char *dev)
 {
-	int fd = -1;
-
-	if (dev) {
-		if (chdir("/dev"))
-			ERROR("failed to change dir to /dev\n");
-		fd = open(dev, O_RDWR);
-		if (chdir("/"))
-			ERROR("failed to change dir to /\n");
-	}
+	int dfd, fd;
 
-	return fd;
-}
+	dfd = open("/dev", O_PATH|O_DIRECTORY);
 
-static int dev_exist(const char *dev)
-{
-	int res;
+	if (dfd < 0)
+		return 0;
+
+	fd = openat(dfd, dev, O_RDONLY);
+	close(dfd);
 
-	res = dev_open(dev);
-	if (res != -1)
-		close(res);
+	if (fd < 0)
+		return 0;
 
-	return (res != -1);
+	close(fd);
+	return 1;
 }
 
 static void fork_worker(struct init_action *a)