change usb control process name to usb-server
[framework/system/system-server.git] / ss_bs.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <limits.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <sysman.h>
26 #include <Ecore_File.h>
27
28 #include "ss_log.h"
29 #include "ss_launch.h"
30
31 #define BSNOTI_DIR              "/opt/bs"
32 #define BSNOTI_FILE             "curbs.log"
33 #define BSNOTI_FULL_PATH        BSNOTI_DIR"/"BSNOTI_FILE
34
35 #define CRASH_WORKER_PATH       "/usr/apps/org.tizen.crash-worker/bin/crash-worker"
36
37 static int noti_fd;
38 static int add_noti(void);
39
40 static int make_noti_file(const char *path, const char *file)
41 {
42         PRT_TRACE("Make Noti File");
43         int fd;
44         char buf[PATH_MAX];
45
46         /* make a directory */
47         if (access(path, F_OK) == -1) {
48                 snprintf(buf, sizeof(buf), "mkdir -p %s", path);
49                 system(buf);
50                 snprintf(buf, sizeof(buf), "chown root:app %s", path);
51                 system(buf);
52         }
53
54         snprintf(buf, sizeof(buf), "%s/%s", path, file);
55
56         if (access(buf, F_OK) == 0)
57                 return -1;
58
59         fd = open(buf, O_CREAT, S_IRUSR | S_IWUSR);
60         close(fd);
61         snprintf(buf, sizeof(buf), "chmod 666 %s/%s", path, file);
62         system(buf);
63         snprintf(buf, sizeof(buf), "chown root:app %s/%s", path, file);
64         system(buf);
65
66         return 0;
67 }
68
69 static void launch_crash_worker(void *data)
70 {
71         FILE *fp;
72         char bsfile_name[NAME_MAX], bs_color[MAX_INPUT];
73         char args[NAME_MAX + MAX_INPUT];
74         int ret = -1, i;
75
76         fp = fopen((char *)data, "r");
77         if (fp == NULL) {
78                 return;
79         }
80         /* launch bs process */
81         while (fgets(args, NAME_MAX + MAX_INPUT, fp) != NULL) {
82                 /* add rule for log */
83                 if (args[strlen(args) - 1] != '\n') {
84                         PRT_TRACE_ERR("bsfile log must be terminated with new line character\n");
85                         break;
86                 }
87                 /* change last caracter from \n to \0 */
88                 args[strlen(args) - 1] = '\0';
89                 for (i = 0; i < NAME_MAX + MAX_INPUT; i++) {
90                         if (args[i] == ' ') {
91                                 if (i >= NAME_MAX - 1) {
92                                         PRT_TRACE_ERR("bsfile name is over 254. 255(NAME_MAX) - 1(NULL Termination)\n");
93                                         break;
94                                 }
95                                 strncpy(bsfile_name, args, i);
96                                 bsfile_name[i] = '\0';
97                                 strncpy(bs_color, args + i + 1, MAX_INPUT);
98                                 bs_color[MAX_INPUT - 1] = '\0';
99                                 snprintf(args, sizeof(args), "%s %s",
100                                          bsfile_name, bs_color);
101                                 PRT_TRACE("bsfile_name(size %d): %s\nargs: %s\n", i, bsfile_name, bs_color, args);
102                                 ret = ss_launch_evenif_exist (CRASH_WORKER_PATH, args);
103                                 break;
104                         }
105                 }
106                 if (ret < 0)
107                         break;
108         }
109         fclose(fp);
110
111         if (ret != -1) {
112                 fp = fopen((char *)data, "w");
113                 if (fp == NULL) {
114                         return;
115                 }
116                 fclose(fp);
117         }
118
119         return;
120 }
121
122 static Ecore_File_Monitor *bs_file_monitor;
123
124 static Ecore_File_Monitor_Cb __bs_file_cb(void *data, Ecore_File_Monitor *em, Ecore_File_Event event, const char *path)
125 {
126         switch (event) {
127         case ECORE_FILE_EVENT_DELETED_DIRECTORY:
128         case ECORE_FILE_EVENT_DELETED_SELF:
129                 if (0 > make_noti_file(BSNOTI_DIR, BSNOTI_FILE)) {
130                         launch_crash_worker((void *)path);
131                 }
132                 break;
133         case ECORE_FILE_EVENT_MODIFIED:
134         default:
135                 launch_crash_worker((void *)path);
136                 break;
137         }
138
139         return;
140 }
141
142 int ss_bs_init(void)
143 {
144         if (0 > make_noti_file(BSNOTI_DIR, BSNOTI_FILE)) {
145                 PRT_TRACE_ERR("make_noti_file() failed");
146                 launch_crash_worker((void *)BSNOTI_FULL_PATH);
147         }
148
149         if (0 == ecore_file_init()) {
150                 PRT_TRACE_ERR("ecore_file_init() failed");
151                 launch_crash_worker((void *)BSNOTI_FULL_PATH);
152         }
153
154         bs_file_monitor = ecore_file_monitor_add(BSNOTI_FULL_PATH,(void *) __bs_file_cb, NULL);
155         if (!bs_file_monitor) {
156                 PRT_TRACE_ERR("ecore_file_monitor_add() failed");
157                 launch_crash_worker((void *)BSNOTI_FULL_PATH);
158                 return -1;
159         }
160
161         return 0;
162 }