1. apply systemd unit files
[framework/appfw/aul-1.git] / am_daemon / amd_status.c
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <glib.h>
24 #include <aul.h>
25 #include <string.h>
26
27 #include "amd_config.h"
28 #include "amd_status.h"
29 #include "aul_util.h"
30 #include "simple_util.h"
31 #include "app_sock.h"
32
33 GSList *app_status_info_list = NULL;
34
35 int _status_add_app_info_list(char *appid, char *app_path, int pid)
36 {
37         GSList *iter = NULL;
38         app_status_info_t *info_t = NULL;
39
40         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
41         {
42                 info_t = (app_status_info_t *)iter->data;
43                 if(pid == info_t->pid) {
44                         return 0;
45                 }
46         }
47
48         info_t = malloc(sizeof(app_status_info_t));
49         strncpy(info_t->appid, appid, MAX_PACKAGE_STR_SIZE-1);
50         strncpy(info_t->app_path, app_path, MAX_PACKAGE_APP_PATH_SIZE-1);
51         info_t->status = STATUS_LAUNCHING;
52         info_t->pid = pid;
53         app_status_info_list = g_slist_append(app_status_info_list, info_t);
54
55         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
56         {
57                 info_t = (app_status_info_t *)iter->data;
58
59                 _D("%s, %d, %d", info_t->appid, info_t->pid, info_t->status);
60         }
61
62         return 0;
63 }
64
65 int _status_update_app_info_list(int pid, int status)
66 {
67         GSList *iter = NULL;
68         app_status_info_t *info_t = NULL;
69
70         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
71         {
72                 info_t = (app_status_info_t *)iter->data;
73                 if(pid == info_t->pid) {
74                         info_t->status = status;
75                         break;
76                 }
77         }
78
79         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
80         {
81                 info_t = (app_status_info_t *)iter->data;
82
83                 _D("%s, %d, %d", info_t->appid, info_t->pid, info_t->status);
84         }
85
86         return 0;
87 }
88
89 int _status_remove_app_info_list(int pid)
90 {
91         GSList *iter = NULL;
92         app_status_info_t *info_t = NULL;
93
94         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
95         {
96                 info_t = (app_status_info_t *)iter->data;
97                 if(pid == info_t->pid) {
98                         app_status_info_list = g_slist_remove(app_status_info_list, info_t);
99                         free(info_t);
100                         break;
101                 }
102         }
103
104         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
105         {
106                 info_t = (app_status_info_t *)iter->data;
107
108                 _D("%s, %d, %d", info_t->appid, info_t->pid, info_t->status);
109         }
110
111         return 0;
112 }
113
114 int _status_app_is_running(char *appid)
115 {
116         GSList *iter = NULL;
117         app_status_info_t *info_t = NULL;
118
119         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
120         {
121                 info_t = (app_status_info_t *)iter->data;
122                 if( strncmp(appid, info_t->appid, MAX_PACKAGE_STR_SIZE) == 0 ) {
123                         return info_t->pid;
124                 }
125         }
126         return -1;
127 }
128
129 int _status_send_running_appinfo(int fd)
130 {
131         GSList *iter = NULL;
132         app_status_info_t *info_t = NULL;
133         app_pkt_t *pkt = NULL;
134         int len;
135         char tmp_pid[MAX_PID_STR_BUFSZ];
136
137         pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
138         if(!pkt) {
139                 _E("malloc fail");
140                 return 0;
141         }
142
143         memset(pkt, 0, AUL_SOCK_MAXBUFF);
144
145         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
146         {
147                 info_t = (app_status_info_t *)iter->data;
148                 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", info_t->pid);
149                 strncat(pkt->data, tmp_pid, MAX_PID_STR_BUFSZ);
150                 strncat(pkt->data, ":", 1);
151                 strncat(pkt->data, info_t->appid, MAX_PACKAGE_STR_SIZE);
152                 strncat(pkt->data, ":", 1);
153                 strncat(pkt->data, info_t->app_path, MAX_PACKAGE_APP_PATH_SIZE);
154                 strncat(pkt->data, ";", 1);
155         }
156
157         pkt->cmd = APP_RUNNING_INFO_RESULT;
158         pkt->len = strlen((char *)pkt->data) + 1;
159
160         if ((len = send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
161                 if (errno == EPIPE)
162                         _E("send failed due to EPIPE.\n");
163                 _E("send fail to client");
164         }
165
166         if(pkt)
167                 free(pkt);
168
169         close(fd);
170
171         return 0;
172 }
173