Delete unused files after process shutdown 89/289289/1
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 3 Mar 2023 04:49:55 +0000 (04:49 +0000)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 6 Mar 2023 01:04:12 +0000 (01:04 +0000)
After the process is terminated, the launchpad deletes unnecessary files.
To debug & profile dotnet applications, the coreclr creates "clr-debug-pipe-"
and "dotnet-diagnostic-" files. The launchpad deletes the files if the files
exists. To avoid blocking the main thread of the launchpad, this patch
creates a new worker thread that is "RecycleBin+".

Change-Id: I68e02359ed2ec79801b46da2f45ef54b5c35f4b9
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/launchpad-process-pool/src/launchpad_signal.c

index 6212ca4390c285ee0c081d9d6f0a50ec948fc6e4..f349d65ffeba73f441e88a9b7e7163bac84502fb 100644 (file)
 #include "launchpad_proc.h"
 #include "launchpad_signal.h"
 #include "launchpad_socket.h"
+#include "launchpad_worker.h"
 #include "log_private.h"
 
 #define HYDRA_SIGCHLD_SOCK          ".hydra-sigchld-sock"
+#define GPOINTER_TO_INT_SAFE(x) ((unsigned long)(x) & UINT32_MAX)
 
 static pid_t __pid;
 static sigset_t __mask;
@@ -44,6 +46,7 @@ static socket_h __hydra_sigchld_socket;
 static io_channel_h __hydra_sigchld_channel;
 static signal_sigchld_cb __callback;
 static void *__user_data;
+static worker_h __recycle_bin;
 
 static gboolean __hydra_sigchld_recovery_cb(gpointer data);
 static gboolean __sigchld_recovery_cb(gpointer data);
@@ -70,11 +73,76 @@ static void __socket_garbage_collector(void)
        closedir(dp);
 }
 
-static void __sigchld_action(int pid)
+static void __delete_unused_files(pid_t pid)
 {
-       _dbus_send_app_dead_signal(pid);
+       DIR *dp;
+       struct dirent *dentry;
+       char file_clr_debug_pipe[128];
+       size_t length_clr_debug_pipe;
+       char file_dotnet_diagnostic[128];
+       size_t length_dotnet_diagnostic;
+       char path[PATH_MAX];
+
+       dp = opendir("/tmp");
+       if (dp == NULL) {
+               _E("opendir() is failed");
+               return;
+       }
+
+       snprintf(file_clr_debug_pipe, sizeof(file_clr_debug_pipe),
+                       "clr-debug-pipe-%d-", pid);
+       length_clr_debug_pipe = strlen(file_clr_debug_pipe);
+       snprintf(file_dotnet_diagnostic, sizeof(file_dotnet_diagnostic),
+                       "dotnet-diagnostic-%d-", pid);
+       length_dotnet_diagnostic = strlen(file_dotnet_diagnostic);
+
+       while ((dentry = readdir(dp)) != NULL) {
+               if (dentry->d_name[0] == '.')
+                       continue;
+
+               if (dentry->d_name[0] == 'c') {
+                       if (length_clr_debug_pipe > strlen(dentry->d_name))
+                               continue;
+
+                       if (!strncmp(file_clr_debug_pipe, dentry->d_name,
+                                               length_clr_debug_pipe)) {
+                               snprintf(path, sizeof(path), "/tmp/%s",
+                                               dentry->d_name);
+                               unlink(path);
+                               _W("unlink(%s)", path);
+                       }
+               } else if (dentry->d_name[0] == 'd') {
+                       if (length_dotnet_diagnostic > strlen(dentry->d_name))
+                               continue;
+
+                       if (!strncmp(file_dotnet_diagnostic, dentry->d_name,
+                                               length_dotnet_diagnostic)) {
+                               snprintf(path, sizeof(path), "/tmp/%s",
+                                               dentry->d_name);
+                               unlink(path);
+                               _W("unlink(%s)", path);
+                       }
+               }
+       }
+       closedir(dp);
+}
+
+static bool __garbage_collector(void* user_data)
+{
+       pid_t pid = GPOINTER_TO_INT_SAFE(user_data);
+
+       _W("pid(%d)", pid);
        _delete_sock_path(pid, getuid());
+       __delete_unused_files(pid);
        __socket_garbage_collector();
+       return false;
+}
+
+static void __sigchld_action(int pid)
+{
+       _dbus_send_app_dead_signal(pid);
+       _worker_add_job(__recycle_bin, __garbage_collector,
+                       GINT_TO_POINTER(pid));
 }
 
 static int __check_permission(int pid)
@@ -393,6 +461,12 @@ int _signal_init(void)
                }
        }
 
+       ret = _worker_create("RecycleBin+", &__recycle_bin);
+       if (ret < 0) {
+               _E("_worker_create() is failed");
+               return ret;
+       }
+
        return 0;
 }
 
@@ -406,6 +480,9 @@ void _signal_fini(void)
 #endif
 
        _D("SIGNAL_FINI");
+       if (getpid() == __pid)
+               _worker_destroy(__recycle_bin);
+
        _signal_set_sigchld_cb(NULL, NULL);
        __hydra_sigchld_fini();
        __sigchld_fini();