Modify login monitor initialization 57/303557/1
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 2 Jan 2024 06:27:04 +0000 (15:27 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 2 Jan 2024 06:27:04 +0000 (15:27 +0900)
After this patch is applied, amd checks whether the /run/systemd path
exists or not. If it does not exist, amd monitors file creation for
waiting until the directory is created.
When the /run/systemd/users is ready or created, amd starts
the sd_login_monitor initialization.

Change-Id: I7943661129669a15eb8b075018ce9015c1f1fa11
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/lib/amd_login_monitor.c

index f2051ae..d16b12f 100644 (file)
@@ -29,6 +29,7 @@
 #include <aul_sock.h>
 #include <vconf.h>
 
+#include "amd_api_inotify.h"
 #include "amd_api_noti.h"
 #include "amd_app_property.h"
 #include "amd_app_status.h"
@@ -45,6 +46,8 @@
 
 #define PATH_AUL_DAEMONS "/run/aul/daemons"
 #define LOGIN_TIMEOUT_SEC 90
+#define FILE_SYSTEMD "systemd"
+#define FILE_USERS "users"
 
 typedef int (*login_cb)(uid_t uid);
 typedef void (*logout_cb)(uid_t uid);
@@ -64,6 +67,8 @@ struct login_monitor_s {
        GIOChannel *io;
        guint sid;
        bool booting_status;
+       amd_inotify_watch_info_h systemd_watch_info;
+       amd_inotify_watch_info_h users_watch_info;
 };
 
 struct user_s {
@@ -746,18 +751,11 @@ static gboolean __monitor_login_cb(GIOChannel *io, GIOCondition condition,
        return TRUE;
 }
 
-static int __init_login_monitor(void)
+static int __init_sd_login_monitor(void)
 {
        int r;
        int fd;
 
-       login_monitor = (struct login_monitor_s *)calloc(1,
-                       sizeof(struct login_monitor_s));
-       if (login_monitor == NULL) {
-               _E("out of memory");
-               return -1;
-       }
-
        r = sd_login_monitor_new("uid", &login_monitor->m);
        if (r < 0) {
                _E("Failed to create sd login monitor");
@@ -786,11 +784,104 @@ static int __init_login_monitor(void)
        return 0;
 }
 
+static gboolean __sd_login_monitor_init_cb(gpointer user_data)
+{
+       static int retrying_count;
+
+       _D("Retrying count=%d", retrying_count);
+       if (__init_sd_login_monitor() < 0) {
+               if (++retrying_count < 10)
+                       return G_SOURCE_CONTINUE;
+       }
+
+       return G_SOURCE_REMOVE;
+}
+
+static bool __monitor_run_systemd_cb(const char *event_name, void *user_data)
+{
+       if (event_name && !strcmp(event_name, FILE_USERS)) {
+               amd_inotify_rm_watch(login_monitor->users_watch_info);
+               login_monitor->users_watch_info = NULL;
+               if (__init_sd_login_monitor() < 0) {
+                       _E("Failed to initialize login monitor");
+                       g_timeout_add(1000, __sd_login_monitor_init_cb, NULL);
+               }
+       }
+
+       return true;
+}
+
+static void __check_and_monitor_run_systemd_users(void)
+{
+       login_monitor->users_watch_info = amd_inotify_add_watch("/run/systemd/",
+                       IN_CREATE, __monitor_run_systemd_cb, NULL);
+       if (login_monitor->users_watch_info == NULL)
+               _E("Failed to add inotify watch");
+
+       if (access("/run/systemd/users", F_OK) == 0) {
+               amd_inotify_rm_watch(login_monitor->users_watch_info);
+               login_monitor->users_watch_info = NULL;
+               if (__init_sd_login_monitor() < 0) {
+                       _E("Failed to initialize login monitor");
+                       g_timeout_add(1000, __sd_login_monitor_init_cb, NULL);
+               }
+       }
+}
+
+static bool __monitor_run_cb(const char *event_name, void *user_data)
+{
+       if (event_name && !strcmp(event_name, FILE_SYSTEMD)) {
+               amd_inotify_rm_watch(login_monitor->systemd_watch_info);
+               login_monitor->systemd_watch_info = NULL;
+               __check_and_monitor_run_systemd_users();
+       }
+
+       return true;
+}
+
+static void __check_and_monitor_run_systemd(void)
+{
+       login_monitor->systemd_watch_info = amd_inotify_add_watch("/run/",
+                       IN_CREATE, __monitor_run_cb, NULL);
+       if (login_monitor->systemd_watch_info == NULL)
+               _E("Failed to add inotify watch");
+
+       if (access("/run/systemd", F_OK) != 0)
+               return;
+
+       amd_inotify_rm_watch(login_monitor->systemd_watch_info);
+       login_monitor->systemd_watch_info = NULL;
+       __check_and_monitor_run_systemd_users();
+}
+
+static int __init_login_monitor(void)
+{
+       login_monitor = (struct login_monitor_s *)calloc(1,
+                       sizeof(struct login_monitor_s));
+       if (login_monitor == NULL) {
+               _E("out of memory");
+               return -1;
+       }
+
+       __check_and_monitor_run_systemd();
+       return 0;
+}
+
 static void __fini_login_monitor(void)
 {
        if (login_monitor == NULL)
                return;
 
+       if (login_monitor->users_watch_info) {
+               amd_inotify_rm_watch(login_monitor->users_watch_info);
+               login_monitor->users_watch_info = NULL;
+       }
+
+       if (login_monitor->systemd_watch_info) {
+               amd_inotify_rm_watch(login_monitor->systemd_watch_info);
+               login_monitor->systemd_watch_info = NULL;
+       }
+
        if (login_monitor->sid) {
                g_source_remove(login_monitor->sid);
                login_monitor->sid = 0;
@@ -1082,7 +1173,7 @@ int _login_monitor_init(void)
        int booting_status;
 
        _D("login monitor init");
-       if (__init_login_monitor()) {
+       if (__init_login_monitor() < 0) {
                _E("Failed to initialize login monitor");
                __fini_login_monitor();
                return -1;