shared: Replace proc operation with that of the libsyscommon 89/295989/2
authorYoungjae Cho <y0.cho@samsung.com>
Mon, 17 Jul 2023 06:20:06 +0000 (15:20 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Tue, 18 Jul 2023 07:02:57 +0000 (07:02 +0000)
Change-Id: I733374a063ab2dee5192cf1c553e1480f9a2343c
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
src/display/display-dbus.c
src/display/display-lock.c
src/power/power-dbus.c
src/power/power-off.c
src/power/power.c
src/shared/common.c
src/shared/common.h
src/tzip/tzip.c

index 05c9a9d..7ebab24 100644 (file)
@@ -27,6 +27,7 @@
 #include <stdbool.h>
 #include <assert.h>
 #include <libsyscommon/libgdbus.h>
+#include <libsyscommon/proc.h>
 #include <device/display.h>
 #include <device/power.h>
 
@@ -1084,7 +1085,7 @@ static GVariant *dbus_locktimeout_expired(GDBusConnection *conn,
                goto out;
        }
 
-       ret = get_command(pid, comm, sizeof(comm));
+       ret = syscommon_proc_get_comm(pid, comm, sizeof(comm));
        if (ret < 0) {
                _E("Failed to get command (%d)", ret);
                goto out;
index fe24f75..87bfae6 100644 (file)
@@ -21,6 +21,8 @@
  * @brief      Manage each state's lock. Lock blocks state transition by timeout.
  *
  */
+#include <libsyscommon/proc.h>
+
 #include "shared/common.h"
 #include "ambient-mode.h"
 #include "device-interface.h"
@@ -311,7 +313,7 @@ static gboolean lock_watchdog_callback(void *data)
                return G_SOURCE_REMOVE;
        }
 
-       if (!is_app(dl->pid)) {
+       if (!syscommon_proc_is_app(dl->pid)) {
                /* For daemon, no need to ask resourced if it is abnormal lock */
                // TODO: is it correct?
                broadcast_lock_watchdog_expired(dl->pid, dl->state, NULL, dl->time);
@@ -785,4 +787,4 @@ int display_lock_request_unlock_with_option(pid_t pid, int s_bits, int flag)
        display_state_transition_do_state_transition_by_pm_control_event(&recv_data);
 
        return 0;
-}
\ No newline at end of file
+}
index 7f68edf..d7e9feb 100644 (file)
@@ -25,6 +25,7 @@
 #include <libsyscommon/libgdbus.h>
 #include <libsyscommon/list.h>
 #include <libsyscommon/file.h>
+#include <libsyscommon/proc.h>
 #include <device/power-internal.h>
 
 #include "shared/devices.h"
@@ -443,7 +444,7 @@ static GVariant *dbus_power_change_state(GDBusConnection *conn,
        if (pid == -1 || kill(pid, 0) == -1)
                goto out;
 
-       get_command(pid, comm, sizeof(comm));
+       syscommon_proc_get_comm(pid, comm, sizeof(comm));
 
        if (is_poweroff_state(next))
                CRITICAL_LOG("Pid=%d(%s) sent request for PowerChangeState to %s", pid, comm, state_name(next));
index 1d4bdec..56369c6 100644 (file)
@@ -37,6 +37,7 @@
 #include <libsyscommon/libgdbus.h>
 #include <libsyscommon/libsystemd.h>
 #include <libsyscommon/ini-parser.h>
+#include <libsyscommon/proc.h>
 
 #include "dd-deviced.h"
 #include "core/log.h"
@@ -357,7 +358,7 @@ static GVariant *dbus_poweroff_handler(GDBusConnection *conn,
        if (ret < 0)
                goto out;
 
-       get_command(ret, comm, sizeof(comm));
+       syscommon_proc_get_comm(ret, comm, sizeof(comm));
 
        if (strncmp(type, "poweroff", sizeof("poweroff")) == 0)
                next = DEVICED_POWER_STATE_POWEROFF;
@@ -393,7 +394,7 @@ static GVariant *dbus_poweroff_option_handler(GDBusConnection *conn,
        if (ret < 0)
                goto out;
 
-       get_command(ret, comm, sizeof(comm));
+       syscommon_proc_get_comm(ret, comm, sizeof(comm));
 
        if (strncmp(type, "poweroff", sizeof("poweroff")) == 0)
                next = DEVICED_POWER_STATE_POWEROFF;
index 6682617..2b00c67 100644 (file)
@@ -23,6 +23,7 @@
 #include <linux/input.h>
 #include <libsyscommon/libgdbus.h>
 #include <libsyscommon/list.h>
+#include <libsyscommon/proc.h>
 #include <device/power-internal.h>
 
 #include "shared/devices.h"
@@ -190,7 +191,7 @@ int add_change_state_wait(pid_t pid, guint64 state)
 
        pi->pid = pid;
        pi->state_bitmap = state;
-       get_command(pid, pi->comm, sizeof(pi->comm));
+       syscommon_proc_get_comm(pid, pi->comm, sizeof(pi->comm));
        proc_list = g_list_append(proc_list, pi);
 
        _D("pid=%d(%s) added csw for %#"PRIx64, pid, pi->comm, state);
index 552c3ad..d87c54e 100644 (file)
 
 #define BUFF_MAX        255
 
-#define APP_ATTR_PATH "/proc/%d/attr/current"
-
-int get_cmdline_name(pid_t pid, char *cmdline, size_t cmdline_size)
-{
-       int fd, ret;
-       char buf[PATH_MAX + 1];
-       char *filename;
-
-       snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
-       fd = open(buf, O_RDONLY);
-       if (fd < 0) {
-               errno = ESRCH;
-               return -1;
-       }
-
-       ret = read(fd, buf, PATH_MAX);
-       close(fd);
-       if (ret < 0)
-               return -1;
-
-       buf[PATH_MAX] = '\0';
-
-       filename = strrchr(buf, '/');
-       if (filename == NULL)
-               filename = buf;
-       else
-               filename = filename + 1;
-
-       if (cmdline_size < strlen(filename) + 1) {
-               errno = EOVERFLOW;
-               return -1;
-       }
-
-       strncpy(cmdline, filename, cmdline_size - 1);
-       cmdline[cmdline_size - 1] = '\0';
-       return 0;
-}
-
-int is_app(pid_t pid)
-{
-       char attr[64];
-       size_t len;
-       int ret;
-
-       ret = get_privilege(pid, attr, sizeof(attr));
-       if (ret < 0) {
-               _E("Failed to get privilege of PID(%d).", pid);
-               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;
-}
-
 /*
  * Helper function
  * - Read from sysfs entry
@@ -261,30 +197,6 @@ void umount_partition_by_kill(const char *path, const int max_retry)
        return;
 }
 
-int get_privilege(pid_t pid, char *name, size_t len)
-{
-       char path[PATH_MAX];
-       char attr[BUFF_MAX];
-       size_t attr_len;
-       FILE *fp;
-
-       snprintf(path, sizeof(path), APP_ATTR_PATH, pid);
-
-       fp = fopen(path, "r");
-       if (!fp)
-               return -errno;
-
-       attr_len = fread(attr, 1, sizeof(attr) - 1, fp);
-       fclose(fp);
-       if (attr_len <= 0)
-               return -ENOENT;
-
-       attr[attr_len] = '\0';
-
-       snprintf(name, len, "%s", attr);
-       return 0;
-}
-
 #define MODEL_NAME      "http://tizen.org/system/model_name"
 #define MODEL_EMULATOR  "Emulator"
 
index 08754f0..12e2814 100644 (file)
@@ -196,18 +196,15 @@ static inline void __cleanup_fclose_func(FILE **f) {
 #define _cleanup_fclose_ _cleanup_(__cleanup_fclose_func)
 #endif
 
-int is_app(pid_t pid);
 int run_child(int argc, const char *argv[]);
 int sys_check_node(char *path);
 int get_systemd_reboot_param(char *buf, unsigned bufsize);
 int mount_check(const char* path);
 void umount_partition_by_kill(const char *path, const int max_retry);
-int get_privilege(pid_t pid, char *name, size_t len);
 bool is_emulator(void);
 int do_mkdir(const char *path, mode_t mode);
 int do_copy_force(const char *src, const char *dst);
 void watchdog_notify(void);
-int get_command(pid_t pid, char *comm, size_t len);
 #ifdef __cplusplus
 
 }
index 72bf41f..4f0602e 100644 (file)
@@ -36,6 +36,7 @@
 #include <tzplatform_config.h>
 #include <libsyscommon/libgdbus.h>
 #include <libsyscommon/list.h>
+#include <libsyscommon/proc.h>
 
 #include "core/log.h"
 #include "shared/devices.h"
@@ -912,7 +913,7 @@ static GVariant *dbus_request_mount_tzip_internal(GDBusConnection *conn, const g
        }
 
        pid = gdbus_connection_get_sender_pid(conn, sender);
-       if (is_app(pid) != 0) {
+       if (syscommon_proc_is_app(pid) != 0) {
                _E("PID(%d) is not privileged to use tzip.", pid);
                ret = -EPERM;
                goto out;
@@ -997,7 +998,7 @@ static GVariant *dbus_request_unmount_tzip_internal(GDBusConnection *conn, const
        }
 
        pid = gdbus_connection_get_sender_pid(conn, sender);
-       if (is_app(pid) != 0) {
+       if (syscommon_proc_is_app(pid) != 0) {
                _E("PID(%d) is not privileged to use tzip.", pid);
                ret = -EPERM;
                goto out;