Revert "Memory leak"
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 12 Nov 2020 02:49:41 +0000 (11:49 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 12 Nov 2020 02:49:46 +0000 (11:49 +0900)
This reverts commit 3bb7ce5f257e83729c9ddf10b6ef9e2db31fbfc0.

Change-Id: I178668dff63f7c9f5bc737dac9b9734ea77f3cea

tool/open_app.c

index 77d71dc..cda801d 100644 (file)
  * limitations under the License.
  */
 
-#define _GNU_SOURCE
-#include <errno.h>
-#include <fcntl.h>
-#include <glib.h>
-#include <limits.h>
-#include <linux/limits.h>
-#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <sys/types.h>
+#include <stdint.h>
 #include <unistd.h>
-#include <bundle_internal.h>
-#include <dlog.h>
+#include <sys/types.h>
+#include <string.h>
+#include <glib.h>
 
+#include <bundle_internal.h>
 
 #include "aul.h"
 
-#undef LOG_TAG
-#define LOG_TAG "DEBUG"
-
-#undef _E
-#define _E LOGE
-
-#undef _W
-#define _W LOGW
+#define ROOT_UID 0
+#define TMP_FILE "/tmp/.testpkg"
 
-#undef _I
-#define _I LOGI
+static char **gargv;
+static int gargc;
+bundle *kb = NULL;
+static int debugFlag = 0;
 
-#undef _D
-#define _D LOGD
+static GMainLoop *mainloop = NULL;
 
-#define auto_ptr(x) __attribute__ ((__cleanup__(x)))
-
-#define PAYLOAD_SIZE (1024 * 1024 * 1000)
-
-static unsigned int __convert_string(const char *str)
+static bundle *create_internal_bundle()
 {
-        while (*str < '0' || *str > '9')
-                str++;
+       bundle *kb;
 
-        return atoi(str);
+       kb = bundle_create();
+       bundle_add(kb, AUL_K_DEBUG, "1");
+       return kb;
 }
 
-static int __open_file(const char *file, FILE **fp)
+int launch(int debug_option)
 {
-        int ret;
-
-        *fp = fopen(file, "r");
-        if (!*fp) {
-                ret = -errno;
-                _E("Failed to open %s. errno(%d)", file, errno);
-                return ret;
-        }
-
-        return 0;
+       int pid;
+
+       if (!debug_option)
+               pid = aul_open_app(gargv[1]);
+       else {
+               kb = create_internal_bundle();
+               if (kb == NULL) {
+                       printf("bundle creation fail\n");
+                       return -1;
+               }
+               pid = aul_launch_app(gargv[1], kb);
+       }
+       return pid;
 }
 
-static void __close_file(FILE **fp)
+void print_usage(char *progname)
 {
-        if (!fp || !*fp)
-                return;
-
-        fclose(*fp);
+       printf("[usage] %s <appid> [-d]\n",
+              progname);
 }
 
-int __proc_get_mem_used_ratio(unsigned int *mem_used_ratio)
+static int __launch_app_dead_handler(int pid, void *data)
 {
-        auto_ptr(__close_file) FILE *fp = NULL;
-        char buf[LINE_MAX];
-        char *str;
-        unsigned int mem_free = 0;
-        unsigned int mem_total = 0;
-        unsigned int mem_available = 0;
-        unsigned int cached = 0;
-        unsigned int used;
-        unsigned int used_ratio;
-        int ret;
-
-        if (!mem_used_ratio) {
-                _E("Invalid parameter");
-                return -EINVAL;
-        }
-
-        ret = __open_file("/proc/meminfo", &fp);
-        if (ret != 0)
-                return ret;
-
-        while (fgets(buf, sizeof(buf), fp) != NULL) {
-                if ((str = strstr(buf, "MemTotal:"))) {
-                        str += strlen("MemTotal:");
-                        mem_total = __convert_string(str);
-                } else if ((str = strstr(buf, "MemFree:"))) {
-                        str += strlen("MemFree:");
-                        mem_free = __convert_string(str);
-                } else if ((str = strstr(buf, "MemAvailable:"))) {
-                        str += strlen("MemAvailable:");
-                        mem_available = __convert_string(str);
-                } else if ((str = strstr(buf, "Cached:")) &&
-                                !strstr(buf, "Swap")) {
-                        str += strlen("Cached:");
-                        cached = atoi(str);
-                        break;
-                }
-        }
-
-        if (mem_total == 0) {
-                _E("Failed to get total memory size");
-                return -1;
-        }
-
-        if (mem_available == 0)
-                mem_available = mem_free + cached;
-
-        used = mem_total - mem_available;
-        used_ratio = used * 100 / mem_total;
-
-        *mem_used_ratio = used_ratio;
-
-        return 0;
-}
+       int listen_pid = (intptr_t)data;
 
-int __proc_get_mem_pss(int pid, unsigned int *mem_pss)
-{
-        auto_ptr(__close_file) FILE *fp = NULL;
-        char path[PATH_MAX];
-        char buf[LINE_MAX];
-        unsigned int pss = 0;
-        unsigned int total_pss = 0;
-        int ret;
-
-        if (pid < 1 || !mem_pss) {
-                _E("Invalid parameter");
-                return -EINVAL;
-        }
-
-        snprintf(path, sizeof(path), "/proc/%d/smaps", pid);
-        ret = __open_file(path, &fp);
-        if (ret != 0)
-                return ret;
-
-        while (fgets(buf, sizeof(buf), fp) != NULL) {
-                if (sscanf(buf, "Pss: %d kB", &pss) == 1) {
-                        total_pss += pss;
-                        pss = 0;
-                }
-        }
-
-        *mem_pss = total_pss;
-
-        return 0;
+       if (listen_pid == pid)
+               g_main_loop_quit(mainloop);
+
+       return 0;
 }
 
-static void __check_mem(void)
+static gboolean run_func(void *data)
 {
-       unsigned int mem_pss = 0;
-       unsigned int mem_used_ratio = 0;
+       int pid;
+       const char *str;
+
+       if ((pid = launch(debugFlag)) > 0)
+               printf("... successfully launched\n");
+       else
+               printf("... launch failed\n");
+
+       if (kb) {
+               str = bundle_get_val(kb, "__LAUNCH_APP_MODE__");
+
+               if (str && strcmp(str, "SYNC") == 0)
+                       aul_listen_app_dead_signal(__launch_app_dead_handler, (void *)(intptr_t)pid);
+               else
+                       g_main_loop_quit(mainloop);
+
+               bundle_free(kb);
+               kb = NULL;
+       } else {
+               g_main_loop_quit(mainloop);
+       }
 
-       __proc_get_mem_used_ratio(&mem_used_ratio);
-       __proc_get_mem_pss(getpid(), &mem_pss);
 
-       printf("== MEM CHECK [START] ==\n");
-       printf(" - memory used ratio: %u %%\n", mem_used_ratio);
-       printf(" - memory pss: %u kB\n", mem_pss);
-       printf("== MEM CHECK [END] ==\n");
+       return TRUE;
 }
 
-static gboolean __timeout_cb(gpointer data)
+
+int main(int argc, char **argv)
 {
-       char *ptr;
-
-       printf("== TIMED OUT [START] ==\n");
-       __check_mem();
-       ptr = malloc(sizeof(char) * PAYLOAD_SIZE);
-       if (!ptr) {
-               fprintf(stderr, "Out of memory\n");
-               printf("== TIMED OUT [END] ==\n");
-               return G_SOURCE_REMOVE;
+       if ((argc < 2) || (argc > 3)) {
+               print_usage(argv[0]);
+               exit(EXIT_FAILURE);
        }
-       printf("== AFTER ALLOCATION ==\n");
-       __check_mem();
-       printf("== TIMED OUT [END] ==\n");
 
-       return G_SOURCE_CONTINUE;
-}
+       gargc = argc;
+       gargv = argv;
 
-int main(int argc, char **argv)
-{
-       GMainLoop *loop;
+       if (argc == 3) {
+               if ((strcmp(argv[2], "-d") != 0) && (strcmp(argv[1], "-d") != 0)) {
+                       printf("additionnal argument should be -d to enable debugging\n");
+                       print_usage(argv[0]);
+                       exit(EXIT_FAILURE);
+               }
+               debugFlag = 1;
+       }
+       aul_launch_init(NULL, NULL);
+
+       g_idle_add(run_func, NULL);
 
-       __check_mem();
-       loop = g_main_loop_new(NULL, FALSE);
-       g_timeout_add(500, __timeout_cb, loop);
-       g_main_loop_run(loop);
-       g_main_loop_unref(loop);
+       mainloop = g_main_loop_new(NULL, FALSE);
+       if (!mainloop) {
+               printf("failed to create glib main loop\n");
+               exit(EXIT_FAILURE);
+       }
+       g_main_loop_run(mainloop);
 
        return 0;
 }
+