tizen 2.3.1 release
[kernel/api/system-resource.git] / src / vip-agent / vip-release-agent.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdarg.h>
4 #include <unistd.h>
5 #include <sys/mount.h>
6 #include <errno.h>
7 #include <sys/wait.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <dirent.h>
12 #include "trace.h"
13 #include "vip-process.h"
14
15 #define TIZEN_DEBUG_MODE_FILE   "/opt/etc/.debugmode"
16 #define DUMP_PATH "/usr/bin/all_log_dump.sh"
17 #define REBOOT_PATH "/usr/sbin/reboot"
18
19 static int check_debugenable(void)
20 {
21         if (access(TIZEN_DEBUG_MODE_FILE, F_OK) == 0)
22                 return 1;
23         else
24                 return 0;
25 }
26
27 static int run_exec(char **argv)
28 {
29         int status = 0;
30         pid_t pid = 0;
31         char buf[256];
32
33         if (argv == NULL)
34                 return -3;
35
36         pid = fork();
37
38         if (pid == -1)
39                 return -1;
40
41         if (pid == 0) {
42                 setpgid(0, 0);
43                 if (execv(argv[0], argv) == -1) {
44                         strerror_r(errno, buf, sizeof(buf));
45                         _E("Error execv: %s.\n", buf);
46                         _exit(-1);
47                 }
48                 _exit(1);
49         }
50         do {
51                 if (waitpid(pid, &status, 0) == -1) {
52                         if (errno != EINTR)
53                                 return -1;
54                 }
55                 else {
56                         return status;
57                 }
58         } while (1);
59 }
60
61 int main(int argc, char *argv[])
62 {
63         int checkfd;
64         char *dumpargv[3] = {DUMP_PATH, NULL, NULL};
65         char *rebootargv[4] = {REBOOT_PATH, "silent", NULL, NULL};
66         DIR *dir = 0;
67
68         dir = opendir(VIP_CGROUP);
69         if (!dir) {
70                 _E("doesn't support cgroup release agent");
71                 return 0;
72         }
73         closedir(dir);
74
75         _E("call release agent : [%d:%s]\n", argc, argv[1]);
76
77         /* check previous process */
78         if (access(CHECK_RELEASE_PROGRESS, F_OK) == 0)
79                 return 0;
80
81         /* make tmp file */
82         checkfd = creat(CHECK_RELEASE_PROGRESS, 0640);
83         if (checkfd < 0) {
84                 _E("fail to make %s file\n", CHECK_RELEASE_PROGRESS);
85                 checkfd = 0;
86         }
87
88         /* unmount cgroup for preventing launching another release-agent */
89         _E("systemd service stop");
90         umount2("/sys/fs/cgroup", MNT_FORCE |MNT_DETACH);
91
92         /* check debug level */
93         if (check_debugenable())
94                 run_exec(dumpargv);
95
96         /* clear tmp file */
97         if (checkfd) {
98                 if (unlink(CHECK_RELEASE_PROGRESS) < 0)
99                         _E("fail to remove %s file\n", CHECK_RELEASE_PROGRESS);
100                 close(checkfd);
101         }
102
103         sync();
104
105         run_exec(rebootargv);
106         return 0;
107 }
108