lcr/0005-add-attach-fd-for-process-state-and-add-needed-util-.patch

219 lines
5.5 KiB
Diff
Raw Normal View History

From d8a706eb0e0ba937d8b99dadbbad2771469eef97 Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Wed, 8 Nov 2023 15:40:01 +0800
Subject: [PATCH 05/13] add attach fd for process state and add needed util
function
Signed-off-by: zhongtao <zhongtao17@huawei.com>
---
.../schema/shim/client/process-state.json | 3 ++
src/utils/utils.c | 48 +++++++++++++++++++
src/utils/utils.h | 2 +
src/utils/utils_file.c | 41 ++++++++++++++++
src/utils/utils_file.h | 4 ++
src/utils/utils_string.c | 17 +++++++
src/utils/utils_string.h | 2 +
7 files changed, 117 insertions(+)
diff --git a/src/json/schema/shim/client/process-state.json b/src/json/schema/shim/client/process-state.json
index f07d14f..2383b43 100644
--- a/src/json/schema/shim/client/process-state.json
+++ b/src/json/schema/shim/client/process-state.json
@@ -151,6 +151,9 @@
"exit_fifo": {
"type": "string"
},
+ "attach_socket": {
+ "type": "string"
+ },
"control_fifo": {
"type": "string"
},
diff --git a/src/utils/utils.c b/src/utils/utils.c
index cd0934f..fabdf71 100644
--- a/src/utils/utils.c
+++ b/src/utils/utils.c
@@ -25,6 +25,9 @@
#include <sys/wait.h>
#include <errno.h>
#include <time.h>
+#include <regex.h>
+
+#include "log.h"
int isula_wait_pid_ret_status(pid_t pid)
{
@@ -81,4 +84,49 @@ void isula_usleep_nointerupt(unsigned long usec)
ret = nanosleep(&request, &remain);
request = remain;
} while (ret == -1 && errno == EINTR);
+}
+
+/*
+ * do not support greedy matching, like: '(:?xx)'
+ * return value:
+ * -1 failed
+ * 0 match
+ * 1 no match
+ */
+int isula_reg_match(const char *patten, const char *str)
+{
+#define EVENT_ARGS_MAX 255
+ int nret = 0;
+ char buffer[EVENT_ARGS_MAX] = { 0 };
+ regex_t reg;
+
+ if (patten == NULL || str == NULL) {
+ ERROR("invalid NULL param");
+ return -1;
+ }
+
+ nret = regcomp(&reg, patten, REG_EXTENDED | REG_NOSUB);
+ if (nret != 0) {
+ regerror(nret, &reg, buffer, EVENT_ARGS_MAX);
+ ERROR("regcomp %s failed: %s", patten, buffer);
+ return -1;
+ }
+
+ nret = regexec(&reg, str, 0, NULL, 0);
+ if (nret == 0) {
+ nret = 0;
+ goto free_out;
+ } else if (nret == REG_NOMATCH) {
+ nret = 1;
+ goto free_out;
+ } else {
+ nret = -1;
+ ERROR("reg match failed");
+ goto free_out;
+ }
+
+free_out:
+ regfree(&reg);
+
+ return nret;
}
\ No newline at end of file
diff --git a/src/utils/utils.h b/src/utils/utils.h
index dabaee2..b4ab9d0 100644
--- a/src/utils/utils.h
+++ b/src/utils/utils.h
@@ -46,6 +46,8 @@ int isula_wait_pid(pid_t pid);
void isula_usleep_nointerupt(unsigned long usec);
+int isula_reg_match(const char *patten, const char *str);
+
#ifdef __cplusplus
}
#endif
diff --git a/src/utils/utils_file.c b/src/utils/utils_file.c
index fb55cf8..067715c 100644
--- a/src/utils/utils_file.c
+++ b/src/utils/utils_file.c
@@ -656,4 +656,45 @@ int isula_path_remove(const char *path)
errno = saved_errno;
}
return -1;
+}
+
+int isula_set_non_block(const int fd)
+{
+ int flag = -1;
+ int ret = -1;
+
+ if (fd < 0) {
+ ERROR("Invalid fd: %d.", fd);
+ return -1;
+ }
+
+ flag = fcntl(fd, F_GETFL, 0);
+ if (flag < 0) {
+ SYSERROR("Failed to get flags for fd: %d", fd);
+ return -1;
+ }
+
+ ret = fcntl(fd, F_SETFL, flag | O_NONBLOCK);
+ if (ret != 0) {
+ SYSERROR("Failed to set flags for fd: %d", fd);
+ return -1;
+ }
+
+ return 0;
+}
+
+int isula_validate_absolute_path(const char *path)
+{
+#define PATTEN_STR "^(/[^/ ]*)+/?$"
+ int nret = 0;
+
+ if (path == NULL) {
+ return -1;
+ }
+
+ if (isula_reg_match(PATTEN_STR, path) != 0) {
+ nret = -1;
+ }
+
+ return nret;
}
\ No newline at end of file
diff --git a/src/utils/utils_file.h b/src/utils/utils_file.h
index 83d0a5d..23c4700 100644
--- a/src/utils/utils_file.h
+++ b/src/utils/utils_file.h
@@ -67,6 +67,10 @@ int isula_file_atomic_write(const char *filepath, const char *content);
int isula_close_inherited_fds(bool closeall, int fd_to_ignore);
+int isula_set_non_block(const int fd);
+
+int isula_validate_absolute_path(const char *path);
+
#ifdef __cplusplus
}
#endif
diff --git a/src/utils/utils_string.c b/src/utils/utils_string.c
index 7b0cbef..863bebe 100644
--- a/src/utils/utils_string.c
+++ b/src/utils/utils_string.c
@@ -375,4 +375,21 @@ isula_string_array *isula_string_array_new(size_t req_init_cap)
ptr->append_arr = isula_string_array_append_array;
return ptr;
+}
+
+bool isula_has_prefix(const char *str, const char *prefix)
+{
+ if (str == NULL || prefix == NULL) {
+ return false;
+ }
+
+ if (strlen(str) < strlen(prefix)) {
+ return false;
+ }
+
+ if (strncmp(str, prefix, strlen(prefix)) != 0) {
+ return false;
+ }
+
+ return true;
}
\ No newline at end of file
diff --git a/src/utils/utils_string.h b/src/utils/utils_string.h
index f403fd9..5a25531 100644
--- a/src/utils/utils_string.h
+++ b/src/utils/utils_string.h
@@ -102,6 +102,8 @@ void isula_string_array_free(isula_string_array *ptr);
isula_string_array *isula_string_split_to_multi(const char *src_str, char delim);
+bool isula_has_prefix(const char *str, const char *prefix);
+
#ifdef __cplusplus
}
#endif
--
2.33.0