From a5760e07d340638e57812d81f6be76d0df479bba Mon Sep 17 00:00:00 2001 From: Yuan Liu Date: Fri, 1 Jun 2018 10:13:47 +0800 Subject: [PATCH] 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. Signed-off-by: Yuan Liu Reviewed-by: Yu Wang Reviewed-by: Zhao Yakui Acked-by: Eddie Dong --- devicemodel/hw/platform/ioc.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/devicemodel/hw/platform/ioc.c b/devicemodel/hw/platform/ioc.c index 24d3c86d2..2897fc62c 100644 --- a/devicemodel/hw/platform/ioc.c +++ b/devicemodel/hw/platform/ioc.c @@ -60,6 +60,7 @@ #include #include #include +#include #include #include @@ -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)