2 * Copyright 2013 Samsung Electronics Co., Ltd
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
8 * http://floralicense.org/license/
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.
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
30 #include <livebox-errno.h>
51 HAPI int liveinfo_init(void)
56 HAPI void liveinfo_fini(void)
58 struct liveinfo *info;
60 EINA_LIST_FREE(s_info.info_list, info) {
61 if (fclose(info->fp) != 0)
62 ErrPrint("fclose: %s\n", strerror(errno));
63 if (unlink(info->fifo_name) < 0)
64 ErrPrint("unlink: %s\n", strerror(errno));
69 static inline int valid_requestor(pid_t pid)
71 char cmdline[60]; /* strlen("/proc/%d/cmdline") + 30 */
75 snprintf(cmdline, sizeof(cmdline), "/proc/%d/exe", pid);
77 DbgPrint("Open cmdline: %s (%d)\n", cmdline, pid);
79 if (stat(cmdline, &target) < 0) {
80 ErrPrint("Error: %s\n", strerror(errno));
84 if (stat("/usr/bin/liveinfo", &src) < 0) {
85 ErrPrint("Error: %s\n", strerror(errno));
89 return target.st_ino == src.st_ino;
92 HAPI struct liveinfo *liveinfo_create(pid_t pid, int handle)
94 struct liveinfo *info;
96 if (!valid_requestor(pid)) {
97 ErrPrint("Invalid requestor\n");
101 info = calloc(1, sizeof(*info));
103 ErrPrint("Heap: %s\n", strerror(errno));
107 snprintf(info->fifo_name, sizeof(info->fifo_name), "/tmp/.live_info.%lf", util_timestamp());
108 if (mkfifo(info->fifo_name, 0644) < 0) {
109 ErrPrint("mkfifo: %s\n", strerror(errno));
110 if (unlink(info->fifo_name) < 0)
111 ErrPrint("unlink: %s\n", strerror(errno));
118 info->handle = handle;
120 DbgPrint("Live info is successfully created\n");
121 s_info.info_list = eina_list_append(s_info.info_list, info);
125 HAPI int liveinfo_open_fifo(struct liveinfo *info)
127 DbgPrint("FIFO is created (%s)\n", info->fifo_name);
128 info->fp = fopen(info->fifo_name, "w");
130 ErrPrint("open: %s\n", strerror(errno));
131 return LB_STATUS_ERROR_IO;
134 return LB_STATUS_SUCCESS;
137 HAPI void liveinfo_close_fifo(struct liveinfo *info)
140 if (fclose(info->fp) != 0)
141 ErrPrint("fclose: %s\n", strerror(errno));
146 HAPI void liveinfo_destroy(struct liveinfo *info)
148 s_info.info_list = eina_list_remove(s_info.info_list, info);
149 liveinfo_close_fifo(info);
150 if (unlink(info->fifo_name) < 0)
151 ErrPrint("unlink: %s\n", strerror(errno));
155 HAPI pid_t liveinfo_pid(struct liveinfo *info)
157 return info ? info->pid : (pid_t)-1;
160 HAPI const char *liveinfo_filename(struct liveinfo *info)
162 return info ? info->fifo_name : NULL;
165 HAPI FILE *liveinfo_fifo(struct liveinfo *info)
167 return info ? info->fp : NULL;
170 HAPI struct liveinfo *liveinfo_find_by_pid(pid_t pid)
173 struct liveinfo *info;
175 EINA_LIST_FOREACH(s_info.info_list, l, info) {
176 if (info->pid == pid)
183 HAPI struct liveinfo *liveinfo_find_by_handle(int handle)
186 struct liveinfo *info;
188 EINA_LIST_FOREACH(s_info.info_list, l, info) {
189 if (info->handle == handle)