61220c8e35ad5f8fc04cae2a7ed7c6aa2da508e5
[platform/core/appfw/pkgmgr-server.git] / src / queue.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 #define _XOPEN_SOURCE 500
19 #include <ftw.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <strings.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27
28 #include <glib.h>
29
30 #include "pkgmgrinfo_type.h"
31 #include "pkgmgr-info.h"
32 #include "pkgmgr-server.h"
33 #include "queue.h"
34
35 #define BACKEND_DIR "/etc/package-manager/backend"
36 #define NOPENFD 20
37
38 struct backend_queue {
39         char *path;
40         char *type;
41         int slot;
42         GList *job_list;
43 };
44
45 static GHashTable *queue_type_table;
46 static GHashTable *queue_slot_table;
47
48 int num_of_backends;
49
50 int _is_queue_empty(int pos)
51 {
52         struct backend_queue *queue;
53
54         queue = (struct backend_queue *)g_hash_table_lookup(queue_slot_table,
55                         (gconstpointer)pos);
56         if (queue == NULL) {
57                 ERR("cannot find queue of slot %d", pos);
58                 return -1;
59         }
60
61         if (queue->job_list == NULL)
62                 return 1;
63
64         return 0;
65 }
66
67 struct backend_job *_pop_queue(int pos)
68 {
69         struct backend_queue *queue;
70         struct backend_job *job;
71         GList *tmp;
72
73         queue = (struct backend_queue *)g_hash_table_lookup(queue_slot_table,
74                         (gconstpointer)pos);
75         if (queue == NULL) {
76                 ERR("cannot find queue of slot %d", pos);
77                 return NULL;
78         }
79
80         tmp = g_list_first(queue->job_list);
81         if (tmp == NULL)
82                 return NULL;
83         job = (struct backend_job *)tmp->data;
84         queue->job_list = g_list_delete_link(queue->job_list, tmp);
85
86         return job;
87 }
88
89 int _push_queue(uid_t target_uid, uid_t caller_uid, const char *req_id,
90                 int req_type, const char *queue_type, const char *pkgid,
91                 const char *args, void *extra_data)
92 {
93         struct backend_queue *queue;
94         struct backend_job *job;
95
96         queue = (struct backend_queue *)g_hash_table_lookup(queue_type_table,
97                         (gconstpointer)queue_type);
98         if (queue == NULL) {
99                 ERR("no such queue: %s", queue_type);
100                 return -1;
101         }
102
103         job = calloc(1, sizeof(struct backend_job));
104         job->target_uid = target_uid;
105         job->caller_uid = caller_uid;
106         if (req_id)
107                 job->req_id = strdup(req_id);
108         job->req_type = req_type;
109         if (pkgid)
110                 job->pkgid = strdup(pkgid);
111         if (args)
112                 job->args = strdup(args);
113         /* just assign pointer value */
114         job->backend_slot = queue->slot;
115         job->backend_type = queue->type;
116         job->backend_path = queue->path;
117         if (extra_data)
118                 job->extra_data = extra_data;
119
120         queue->job_list = g_list_append(queue->job_list, (gpointer)job);
121
122         return 0;
123 }
124
125 static int __init_backends(const char *fpath, const struct stat *sb,
126                 int typeflag, struct FTW *ftwbuf)
127 {
128         int r;
129         struct backend_queue *queue;
130
131         if (typeflag != FTW_F && typeflag != FTW_SL)
132                 return 0;
133
134         queue = calloc(1, sizeof(struct backend_queue));
135         if (typeflag == FTW_F) {
136                 queue->path = strdup(fpath);
137         } else if (typeflag == FTW_SL) {
138                 queue->path = malloc(sb->st_size + 1);
139                 r = readlink(fpath, queue->path, sb->st_size + 1);
140                 if (r < 0 || r > sb->st_size) {
141                         ERR("failed to readlink for %s", fpath);
142                         free(queue->path);
143                         free(queue);
144                         return -1;
145                 }
146                 queue->path[r] = '\0';
147         }
148         queue->type = strdup(fpath + ftwbuf->base);
149         queue->slot = num_of_backends++;
150         g_hash_table_insert(queue_type_table, (gpointer)queue->type,
151                         (gpointer)queue);
152         g_hash_table_insert(queue_slot_table, (gpointer)queue->slot,
153                         (gpointer)queue);
154
155         DBG("backend detected: %s(%s)", queue->type, queue->path);
156
157         return 0;
158 }
159
160 static gboolean __str_equal(gconstpointer v1, gconstpointer v2)
161 {
162         const char *str1 = (const char *)v1;
163         const char *str2 = (const char *)v2;
164
165         /* ignore case */
166         return strcasecmp(str1, str2) == 0;
167 }
168
169 void __free_extra_info(struct backend_job *job)
170 {
171         struct getsize_sync_extra_info *getsize_sync_data;
172         pkgmgrinfo_updateinfo_h update_info;
173
174         switch (job->req_type) {
175         case REQUEST_TYPE_GETSIZE_SYNC:
176                 getsize_sync_data = (struct getsize_sync_extra_info *)job->extra_data;
177                 if (!getsize_sync_data)
178                         break;
179                 if (getsize_sync_data->getsize_io)
180                         g_io_channel_unref(getsize_sync_data->getsize_io);
181                 if (getsize_sync_data->getsize_fd)
182                         close(getsize_sync_data->getsize_fd);
183                 if (getsize_sync_data->getsize_fifo) {
184                         unlink(getsize_sync_data->getsize_fifo);
185                         free(getsize_sync_data->getsize_fifo);
186                 }
187                 free(getsize_sync_data);
188                 job->extra_data = NULL;
189                 break;
190         case REQUEST_TYPE_REGISTER_PKG_UPDATE_INFO:
191                 update_info = (pkgmgrinfo_updateinfo_h)job->extra_data;
192                 if (!update_info)
193                         break;
194                 pkgmgrinfo_updateinfo_destroy(update_info);
195                 job->extra_data = NULL;
196                 break;
197         }
198 }
199
200 void _free_backend_job(struct backend_job *job)
201 {
202         __free_extra_info(job);
203         free(job->req_id);
204         free(job->pkgid);
205         free(job->appid);
206         free(job->args);
207         free(job);
208 }
209
210 static void __free_backend_queue(gpointer data)
211 {
212         struct backend_queue *queue = (struct backend_queue *)data;
213
214         g_list_free_full(queue->job_list, (GDestroyNotify)_free_backend_job);
215         free(queue->path);
216         free(queue->type);
217         free(queue);
218 }
219
220 void _fini_backend_queue(void)
221 {
222         g_hash_table_destroy(queue_slot_table);
223         g_hash_table_destroy(queue_type_table);
224 }
225
226 int _init_backend_queue(void)
227 {
228         queue_type_table = g_hash_table_new_full(g_str_hash, __str_equal,
229                         NULL, __free_backend_queue);
230         queue_slot_table = g_hash_table_new(g_direct_hash, g_direct_equal);
231
232         if (nftw(BACKEND_DIR, __init_backends, NOPENFD, FTW_PHYS))
233                 return -1;
234
235         DBG("number of backends: %d", num_of_backends);
236
237         return 0;
238 }