From 928f87047a86a7f0f4cf02b92e85094543829e89 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Mon, 17 Jul 2023 15:18:40 +0900 Subject: [PATCH] proc: Add proc operation These have come from the deviced. - int syscommon_proc_get_comm(pid_t pid, char *buf, int len); - int syscommon_proc_get_cmdline(pid_t pid, char *buf, int len); - int syscommon_proc_get_attr_current(pid_t pid, char *buf, int len); - int syscommon_proc_is_app(pid_t pid); Change-Id: I89b668ed045db9c666e1921bceaf8a70c112af78 Signed-off-by: Youngjae Cho --- CMakeLists.txt | 1 + include/libsyscommon/proc.h | 33 ++++++++++++ src/libcommon/proc.c | 103 ++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 include/libsyscommon/proc.h create mode 100644 src/libcommon/proc.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d0a82d..9094e74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ SET(libsyscommon_SRCS src/libcommon/file.c src/libcommon/common.c src/libcommon/bitmap.c + src/libcommon/proc.c src/resource-manager/resource-manager.c src/resource-manager/resource-device.c src/resource-manager/resource-listener-epoll.c diff --git a/include/libsyscommon/proc.h b/include/libsyscommon/proc.h new file mode 100644 index 0000000..e6d2f3a --- /dev/null +++ b/include/libsyscommon/proc.h @@ -0,0 +1,33 @@ +/* MIT License + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ + +#ifndef __SYSCOMMON_PROC_H__ +#define __SYSCOMMON_PROC_H__ + +#include + +int syscommon_proc_get_comm(pid_t pid, char *buf, int len); +int syscommon_proc_get_cmdline(pid_t pid, char *buf, int len); +int syscommon_proc_get_attr_current(pid_t pid, char *buf, int len); +int syscommon_proc_is_app(pid_t pid); + +#endif /*__SYSCOMMON_PROC_H__ */ diff --git a/src/libcommon/proc.c b/src/libcommon/proc.c new file mode 100644 index 0000000..16c439f --- /dev/null +++ b/src/libcommon/proc.c @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include + +#include "shared/log.h" +#include "libsyscommon/proc.h" + +#define PATH_MAX 128 +#define NAME_MAX 64 +#define FORMAT_MAX 16 + +static int proc_get_string(char *buf, int len, const char *pathfmt, ...) +{ + FILE *fp = NULL; + char path[PATH_MAX] = { 0 , }; + char scanfmt[FORMAT_MAX] = { 0, }; + va_list ap; + int ret = 0; + + if (!buf || len < 0) + return -EINVAL; + + va_start(ap, pathfmt); + ret = vsnprintf(path, PATH_MAX, pathfmt, ap); + va_end(ap); + + if (ret < 0) + return -EIO; + + if (ret >= PATH_MAX) + return -ENAMETOOLONG; + + fp = fopen(path, "r"); + if (!fp) { + _E("Failed to open %s, %m", path); + return -errno; + } + + ret = snprintf(scanfmt, FORMAT_MAX, "%%%ds", len - 1); + if (ret < 0) { + fclose(fp); + return -EIO; + } + + if (ret >= FORMAT_MAX) { /* impossible */ + fclose(fp); + return -ERANGE; + } + + errno = 0; + ret = fscanf(fp, scanfmt, buf); + if (ret != 1) { + fclose(fp); + return errno ? -errno : -EIO; + } + + fclose(fp); + + return 0; +} + +int syscommon_proc_get_comm(pid_t pid, char *buf, int len) +{ + return proc_get_string(buf, len, "/proc/%d/comm", pid); +} + +int syscommon_proc_get_cmdline(pid_t pid, char *buf, int len) +{ + return proc_get_string(buf, len, "/proc/%d/cmdline", pid); +} + +int syscommon_proc_get_attr_current(pid_t pid, char *buf, int len) +{ + return proc_get_string(buf, len, "/proc/%d/attr/current", pid); +} + +int syscommon_proc_is_app(pid_t pid) +{ + char attr[NAME_MAX] = { 0 ,}; + size_t len = 0; + int ret = 0; + + ret = syscommon_proc_get_attr_current(pid, attr, sizeof(attr)); + if (ret != 0) { + _E("Failed to read privilege, %d", ret); + return -1; + } + + len = strlen(attr) + 1; + + if (!strncmp("System", attr, len)) + return 0; + + if (!strncmp("User", attr, len)) + return 0; + + if (!strncmp("System::Privileged", attr, len)) + return 0; + + return 1; +} -- 2.34.1