IOC mediator: add check_dir function to avoid symbol link failure
To avoid PTY device symbol link failure due to non-exist directory passed from
parameter. Add check_dir function to check the directory and create it if not
exist.
Acked-by: Eddie Dong
#include <string.h>
#include <stdbool.h>
#include <types.h>
+#include <libgen.h>
#include <sys/stat.h>
#include <sys/types.h>
@@ -552,6 +553,33 @@ ioc_open_native_ch(const char *dev_name)
return fd;
}
+/*
+ * Check and create the directory.
+ * To avoid symlink failure if the directory does not exist.
+ */
+static int
+check_dir(const char *file)
+{
+ char *tmp, *dir;
+
+ tmp = strdup(file);
+ if (!tmp) {
+ DPRINTF("ioc falied to dup file, error:%s\r\n",
+ strerror(errno));
+ return -1;
+ }
+
+ dir = dirname(tmp);
+ if (access(dir, F_OK) && mkdir(dir, 0666)) {
+ DPRINTF("ioc falied to create dir:%s, erorr:%s\r\n", dir,
+ strerror(errno));
+ free(tmp);
+ return -1;
+ }
+ free(tmp);
+ return 0;
+}
+
/*
* Open PTY master device for IOC mediator and the PTY slave device for virtual
* UART. The pair(master/slave) can work as a communication channel between
@@ -576,6 +604,12 @@ ioc_open_virtual_uart(const char *dev_name)
goto pty_err;
if ((unlink(dev_name) < 0) && errno != ENOENT)
goto pty_err;
+ /*
+ * The check_dir restriction is that only create one directory
+ * not support multi-level directroy.
+ */
+ if (check_dir(dev_name) < 0)
+ goto pty_err;
if (symlink(slave_name, dev_name) < 0)
goto pty_err;
if (chmod(dev_name, 0660) < 0)
