Run key-manager-listener by systemd instead key-manager.
[platform/core/security/key-manager.git] / src / listener / listener-daemon.cpp
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <errno.h>
5 #include <unistd.h>
6 #include <syslog.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <systemd/sd-daemon.h>
11
12 #include <glib.h>
13 #include <package_manager.h>
14 #include <ckm/ckm-control.h>
15 #include <dlog.h>
16
17 #define CKM_TAG "CKM_LISTENER"
18
19 void eventCallback(
20     const char *type,
21     const char *package,
22     package_manager_event_type_e eventType,
23     package_manager_event_state_e eventState,
24     int progress,
25     package_manager_error_e error,
26     void *userData)
27 {
28     (void) type;
29     (void) progress;
30     (void) error;
31     (void) userData;
32
33     if (eventType != PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL)
34         return;
35
36     if (eventState != PACKAGE_MANAGER_EVENT_STATE_STARTED)
37         return;
38
39     if (package == NULL)
40         return;
41
42     SLOG(LOG_DEBUG, CKM_TAG, "Get callback. Uninstalation of: %s", package);
43     auto control = CKM::Control::create();
44     control->removeApplicationData(std::string(package));
45 }
46
47 int main(void) {
48     SLOG(LOG_DEBUG, CKM_TAG, "%s", "Start!");
49
50     // Let's operate in background
51     int result = fork();
52     if (result < 0){
53         SLOG(LOG_DEBUG, CKM_TAG, "%s", "Error in fork!");
54         exit(1);
55     }
56
57     if (result > 0)
58         exit(0);
59
60     // Let's disconnect from terminal
61     if (-1 == setsid()) {
62         SLOG(LOG_DEBUG, CKM_TAG, "%s", "Error in fork!");
63         exit(1);
64     }
65
66     // Let's close all descriptors
67 //    for (result = getdtablesize(); result>=0; --result)
68 //        close(result);
69
70     close(0);
71     close(1);
72     close(2);
73
74     result = open("/dev/null", O_RDWR); // open stdin
75
76     int fd_stdout = 0;
77     int fd_stderr = 0;
78     fd_stdout = dup(result); // stdout
79     fd_stderr = dup(result); // stderr
80     SLOG(LOG_DEBUG, CKM_TAG, "%d : %s", fd_stdout, "stdout file descriptor");
81     SLOG(LOG_DEBUG, CKM_TAG, "%d : %s", fd_stderr, "stderr file descriptor");
82
83
84     umask(027);
85
86     // Let's change current directory
87     if (-1 == chdir("/")) {
88         SLOG(LOG_DEBUG, CKM_TAG, "%s", "Error in chdir!");
89         exit(1);
90     }
91
92     // Let's create lock file
93     result = open("/tmp/ckm-listener.lock", O_RDWR | O_CREAT, 0640);
94     if (result < 0) {
95         SLOG(LOG_DEBUG, CKM_TAG, "%s", "Error in opening lock file!");
96         exit(1);
97     }
98
99     if (lockf(result, F_TLOCK, 0) < 0) {
100         SLOG(LOG_DEBUG, CKM_TAG, "%s", "Daemon already working!");
101         exit(0);
102     }
103
104     char str[100];
105     sprintf(str, "%d\n", getpid());
106     result = write(result, str, strlen(str));
107
108     SLOG(LOG_DEBUG, CKM_TAG, "%s", str);
109
110     // Let's start to listen
111     GMainLoop *main_loop = g_main_loop_new(NULL, FALSE);
112     package_manager_h request;
113
114     package_manager_create(&request);
115     if (0 != package_manager_set_event_cb(request, eventCallback, NULL)) {
116         SLOG(LOG_DEBUG, CKM_TAG, "%s", "Error in package_manager_set_event_cb");
117         exit(-1);
118     }
119
120     /* Change file mode mask */
121
122     SLOG(LOG_DEBUG, CKM_TAG, "%s", "Ready to listen!");
123     g_main_loop_run(main_loop);
124     return 0;
125 }
126