Various patches are applied
[platform/framework/web/data-provider-master.git] / src / liveinfo.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.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 #include <stdio.h>
18 #include <stdlib.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <libgen.h>
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26
27 #include <Eina.h>
28
29 #include <dlog.h>
30 #include <livebox-errno.h>
31
32 #include "util.h"
33 #include "debug.h"
34 #include "conf.h"
35
36 int errno;
37
38 static struct info {
39         Eina_List *info_list;
40 } s_info = {
41         .info_list = NULL,
42 };
43
44 struct liveinfo {
45         FILE *fp;
46         char fifo_name[60];
47         pid_t pid;
48         int handle;
49 };
50
51 HAPI int liveinfo_init(void)
52 {
53         return 0;
54 }
55
56 HAPI void liveinfo_fini(void)
57 {
58         struct liveinfo *info;
59
60         EINA_LIST_FREE(s_info.info_list, info) {
61                 fclose(info->fp);
62                 unlink(info->fifo_name);
63                 DbgFree(info);
64         }
65 }
66
67 static inline int valid_requestor(pid_t pid)
68 {
69         char cmdline[60]; /* strlen("/proc/%d/cmdline") + 30 */
70         struct stat target;
71         struct stat src;
72
73         snprintf(cmdline, sizeof(cmdline), "/proc/%d/exe", pid);
74
75         DbgPrint("Open cmdline: %s (%d)\n", cmdline, pid);
76
77         if (stat(cmdline, &target) < 0) {
78                 ErrPrint("Error: %s\n", strerror(errno));
79                 return 0;
80         }
81
82         if (stat("/usr/bin/liveinfo", &src) < 0) {
83                 ErrPrint("Error: %s\n", strerror(errno));
84                 return 0;
85         }
86
87         return target.st_ino == src.st_ino;
88 }
89
90 HAPI struct liveinfo *liveinfo_create(pid_t pid, int handle)
91 {
92         struct liveinfo *info;
93
94         if (!valid_requestor(pid)) {
95                 ErrPrint("Invalid requestor\n");
96                 return NULL;
97         }
98
99         info = calloc(1, sizeof(*info));
100         if (!info) {
101                 ErrPrint("Heap: %s\n", strerror(errno));
102                 return NULL;
103         }
104
105         snprintf(info->fifo_name, sizeof(info->fifo_name), "/tmp/.live_info.%lf", util_timestamp());
106         if (mkfifo(info->fifo_name, 0644) < 0) {
107                 ErrPrint("mkfifo: %s\n", strerror(errno));
108                 unlink(info->fifo_name);
109                 DbgFree(info);
110                 return NULL;
111         }
112
113         info->fp = NULL;
114         info->pid = pid;
115         info->handle = handle;
116
117         DbgPrint("Live info is successfully created\n");
118         s_info.info_list = eina_list_append(s_info.info_list, info);
119         return info;
120 }
121
122 HAPI int liveinfo_open_fifo(struct liveinfo *info)
123 {
124         DbgPrint("FIFO is created (%s)\n", info->fifo_name);
125         info->fp = fopen(info->fifo_name, "w");
126         if (!info->fp) {
127                 ErrPrint("open: %s\n", strerror(errno));
128                 return LB_STATUS_ERROR_IO;
129         }
130
131         return LB_STATUS_SUCCESS;
132 }
133
134 HAPI void liveinfo_close_fifo(struct liveinfo *info)
135 {
136         if (info->fp) {
137                 fclose(info->fp);
138                 info->fp = NULL;
139         }
140 }
141
142 HAPI void liveinfo_destroy(struct liveinfo *info)
143 {
144         s_info.info_list = eina_list_remove(s_info.info_list, info);
145         liveinfo_close_fifo(info);
146         unlink(info->fifo_name);
147         DbgFree(info);
148 }
149
150 HAPI pid_t liveinfo_pid(struct liveinfo *info)
151 {
152         return info ? info->pid : (pid_t)-1;
153 }
154
155 HAPI const char *liveinfo_filename(struct liveinfo *info)
156 {
157         return info ? info->fifo_name : NULL;
158 }
159
160 HAPI FILE *liveinfo_fifo(struct liveinfo *info)
161 {
162         return info ? info->fp : NULL;
163 }
164
165 HAPI struct liveinfo *liveinfo_find_by_pid(pid_t pid)
166 {
167         Eina_List *l;
168         struct liveinfo *info;
169
170         EINA_LIST_FOREACH(s_info.info_list, l, info) {
171                 if (info->pid == pid)
172                         return info;
173         }
174
175         return NULL;
176 }
177
178 HAPI struct liveinfo *liveinfo_find_by_handle(int handle)
179 {
180         Eina_List *l;
181         struct liveinfo *info;
182
183         EINA_LIST_FOREACH(s_info.info_list, l, info) {
184                 if (info->handle == handle)
185                         return info;
186         }
187
188         return NULL;
189 }
190
191 /* End of a file */