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 <yuan1.liu@intel.com>
Reviewed-by: Yu Wang <yu1.wang@intel.com>
Reviewed-by: Zhao Yakui <yakui.zhao@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
This commit is contained in:
Yuan Liu 2018-06-01 10:13:47 +08:00 committed by lijinxia
parent d904202cfb
commit a5760e07d3

View File

@ -60,6 +60,7 @@
#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)