Skip to content

Commit a5760e0

Browse files
yliu80lijinxia
authored andcommitted
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>
1 parent d904202 commit a5760e0

File tree

1 file changed

+34
-0
lines changed
  • devicemodel/hw/platform

1 file changed

+34
-0
lines changed

devicemodel/hw/platform/ioc.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include <string.h>
6161
#include <stdbool.h>
6262
#include <types.h>
63+
#include <libgen.h>
6364

6465
#include <sys/stat.h>
6566
#include <sys/types.h>
@@ -552,6 +553,33 @@ ioc_open_native_ch(const char *dev_name)
552553
return fd;
553554
}
554555

556+
/*
557+
* Check and create the directory.
558+
* To avoid symlink failure if the directory does not exist.
559+
*/
560+
static int
561+
check_dir(const char *file)
562+
{
563+
char *tmp, *dir;
564+
565+
tmp = strdup(file);
566+
if (!tmp) {
567+
DPRINTF("ioc falied to dup file, error:%s\r\n",
568+
strerror(errno));
569+
return -1;
570+
}
571+
572+
dir = dirname(tmp);
573+
if (access(dir, F_OK) && mkdir(dir, 0666)) {
574+
DPRINTF("ioc falied to create dir:%s, erorr:%s\r\n", dir,
575+
strerror(errno));
576+
free(tmp);
577+
return -1;
578+
}
579+
free(tmp);
580+
return 0;
581+
}
582+
555583
/*
556584
* Open PTY master device for IOC mediator and the PTY slave device for virtual
557585
* UART. The pair(master/slave) can work as a communication channel between
@@ -576,6 +604,12 @@ ioc_open_virtual_uart(const char *dev_name)
576604
goto pty_err;
577605
if ((unlink(dev_name) < 0) && errno != ENOENT)
578606
goto pty_err;
607+
/*
608+
* The check_dir restriction is that only create one directory
609+
* not support multi-level directroy.
610+
*/
611+
if (check_dir(dev_name) < 0)
612+
goto pty_err;
579613
if (symlink(slave_name, dev_name) < 0)
580614
goto pty_err;
581615
if (chmod(dev_name, 0660) < 0)

0 commit comments

Comments
 (0)