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) {
62 unlink(info->fifo_name);
67 static inline int valid_requestor(pid_t pid)
69 char cmdline[60]; /* strlen("/proc/%d/cmdline") + 30 */
73 snprintf(cmdline, sizeof(cmdline), "/proc/%d/exe", pid);
75 DbgPrint("Open cmdline: %s (%d)\n", cmdline, pid);
77 if (stat(cmdline, &target) < 0) {
78 ErrPrint("Error: %s\n", strerror(errno));
82 if (stat("/usr/bin/liveinfo", &src) < 0) {
83 ErrPrint("Error: %s\n", strerror(errno));
87 return target.st_ino == src.st_ino;
90 HAPI struct liveinfo *liveinfo_create(pid_t pid, int handle)
92 struct liveinfo *info;
94 if (!valid_requestor(pid)) {
95 ErrPrint("Invalid requestor\n");
99 info = calloc(1, sizeof(*info));
101 ErrPrint("Heap: %s\n", strerror(errno));
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);
115 info->handle = handle;
117 DbgPrint("Live info is successfully created\n");
118 s_info.info_list = eina_list_append(s_info.info_list, info);
122 HAPI int liveinfo_open_fifo(struct liveinfo *info)
124 DbgPrint("FIFO is created (%s)\n", info->fifo_name);
125 info->fp = fopen(info->fifo_name, "w");
127 ErrPrint("open: %s\n", strerror(errno));
128 return LB_STATUS_ERROR_IO;
131 return LB_STATUS_SUCCESS;
134 HAPI void liveinfo_close_fifo(struct liveinfo *info)
142 HAPI void liveinfo_destroy(struct liveinfo *info)
144 s_info.info_list = eina_list_remove(s_info.info_list, info);
145 liveinfo_close_fifo(info);
146 unlink(info->fifo_name);
150 HAPI pid_t liveinfo_pid(struct liveinfo *info)
152 return info ? info->pid : (pid_t)-1;
155 HAPI const char *liveinfo_filename(struct liveinfo *info)
157 return info ? info->fifo_name : NULL;
160 HAPI FILE *liveinfo_fifo(struct liveinfo *info)
162 return info ? info->fp : NULL;
165 HAPI struct liveinfo *liveinfo_find_by_pid(pid_t pid)
168 struct liveinfo *info;
170 EINA_LIST_FOREACH(s_info.info_list, l, info) {
171 if (info->pid == pid)
178 HAPI struct liveinfo *liveinfo_find_by_handle(int handle)
181 struct liveinfo *info;
183 EINA_LIST_FOREACH(s_info.info_list, l, info) {
184 if (info->handle == handle)