~ubuntu-branches/ubuntu/trusty/modem-manager-gui/trusty-backports

« back to all changes in this revision

Viewing changes to src/libpaths.c

  • Committer: Package Import Robot
  • Author(s): Graham Inggs
  • Date: 2013-07-30 12:51:59 UTC
  • Revision ID: package-import@ubuntu.com-20130730125159-flzv882fhuzhmfmi
Tags: upstream-0.0.16
ImportĀ upstreamĀ versionĀ 0.0.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      libpaths.c
 
3
 *      
 
4
 *      Copyright 2013 Alex <alex@linuxonly.ru>
 
5
 *      
 
6
 *      This program is free software: you can redistribute it and/or modify
 
7
 *      it under the terms of the GNU General Public License as published by
 
8
 *      the Free Software Foundation; either version 3 of the License, or
 
9
 *      (at your option) any later version.
 
10
 *      
 
11
 *      This program is distributed in the hope that it will be useful,
 
12
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *      GNU General Public License for more details.
 
15
 *      
 
16
 *      You should have received a copy of the GNU General Public License
 
17
 *      along with this program. If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include <glib.h>
 
21
#include <stdio.h>
 
22
#include <stdarg.h>
 
23
#include <sys/mman.h>
 
24
#include <sys/stat.h>
 
25
#include <fcntl.h>
 
26
#include <stdlib.h>
 
27
#include <unistd.h>
 
28
#include <ctype.h>
 
29
#include <string.h>
 
30
 
 
31
#include "libpaths.h"
 
32
 
 
33
#define MMGUI_LIBPATHS_CACHE_FILE         "/etc/ld.so.cache"
 
34
#define MMGUI_LIBPATHS_CACHE_SOEXT        ".so"
 
35
#define MMGUI_LIBPATHS_CACHE_LIB_TEMP     "/usr/lib/%s.so"
 
36
#define MMGUI_LIBPATHS_CACHE_PATH_TEMP    "/usr/lib/%s.so"
 
37
/*Cache file*/
 
38
#define MMGUI_LIBPATHS_LOCAL_CACHE_XDG    ".cache"
 
39
#define MMGUI_LIBPATHS_LOCAL_CACHE_DIR    "modem-manager-gui"
 
40
#define MMGUI_LIBPATHS_LOCAL_CACHE_FILE   "libpaths.conf"
 
41
#define MMGUI_LIBPATHS_LOCAL_CACHE_PERM   0755
 
42
/*Cache file sections*/
 
43
#define MMGUI_LIBPATHS_FILE_ROOT_SECTION  "cache"
 
44
#define MMGUI_LIBPATHS_FILE_TIMESTAMP     "timestamp"
 
45
#define MMGUI_LIBPATHS_FILE_NAME          "name"
 
46
#define MMGUI_LIBPATHS_FILE_PATH          "path"
 
47
 
 
48
 
 
49
struct _mmgui_libpaths_entry {
 
50
        gchar *id;
 
51
        gchar *libname;
 
52
        gchar *libpath;
 
53
};
 
54
 
 
55
typedef struct _mmgui_libpaths_entry *mmgui_libpaths_entry_t;
 
56
 
 
57
 
 
58
static gboolean mmgui_libpaths_cache_open_local_cache_file(mmgui_libpaths_cache_t libcache, guint64 dbtimestamp)
 
59
{
 
60
        const gchar *homepath; 
 
61
        gchar *confpath;
 
62
        guint64 localtimestamp;
 
63
        GError *error;
 
64
        
 
65
        if (libcache == NULL) return FALSE;
 
66
        
 
67
        homepath = g_get_home_dir();
 
68
        
 
69
        if (homepath == NULL) return FALSE;
 
70
        
 
71
        confpath = g_build_filename(homepath, MMGUI_LIBPATHS_LOCAL_CACHE_XDG, MMGUI_LIBPATHS_LOCAL_CACHE_DIR, NULL);
 
72
        
 
73
        if (g_mkdir_with_parents(confpath, MMGUI_LIBPATHS_LOCAL_CACHE_PERM) != 0) {
 
74
                g_debug("No write access to program settings directory");
 
75
                g_free(confpath);
 
76
                return FALSE;
 
77
        }
 
78
        
 
79
        g_free(confpath);
 
80
        
 
81
        libcache->localfilename = g_build_filename(homepath, MMGUI_LIBPATHS_LOCAL_CACHE_XDG, MMGUI_LIBPATHS_LOCAL_CACHE_DIR, MMGUI_LIBPATHS_LOCAL_CACHE_FILE, NULL);
 
82
        
 
83
        libcache->localkeyfile = g_key_file_new();
 
84
        
 
85
        error = NULL;
 
86
        
 
87
        if (!g_key_file_load_from_file(libcache->localkeyfile, libcache->localfilename, G_KEY_FILE_NONE, &error)) {
 
88
                libcache->updatelocal = TRUE;
 
89
                g_debug("Local cache file loading error: %s", error->message);
 
90
                g_error_free(error);
 
91
        } else {
 
92
                error = NULL;
 
93
                if (g_key_file_has_key(libcache->localkeyfile, MMGUI_LIBPATHS_FILE_ROOT_SECTION, MMGUI_LIBPATHS_FILE_TIMESTAMP, &error)) {
 
94
                        error = NULL;
 
95
                        localtimestamp = g_key_file_get_uint64(libcache->localkeyfile, MMGUI_LIBPATHS_FILE_ROOT_SECTION, MMGUI_LIBPATHS_FILE_TIMESTAMP, &error);
 
96
                        if (error == NULL) {
 
97
                                if (localtimestamp == dbtimestamp) {
 
98
                                        libcache->updatelocal = FALSE;
 
99
                                } else {
 
100
                                        libcache->updatelocal = TRUE;
 
101
                                }
 
102
                        } else {
 
103
                                libcache->updatelocal = TRUE;
 
104
                                g_debug("Local cache contain unreadable timestamp: %s", error->message);
 
105
                                g_error_free(error);
 
106
                        }
 
107
                } else {
 
108
                        libcache->updatelocal = TRUE;
 
109
                        g_debug("Local cache does not contain timestamp: %s", error->message);
 
110
                        g_error_free(error);
 
111
                }
 
112
        }
 
113
        
 
114
        return !libcache->updatelocal;
 
115
}
 
116
 
 
117
static gboolean mmgui_libpaths_cache_close_local_cache_file(mmgui_libpaths_cache_t libcache, gboolean update)
 
118
{
 
119
        gchar *filedata;
 
120
        gsize datasize;
 
121
        GError *error;
 
122
        
 
123
        if (libcache == NULL) return FALSE;
 
124
        if ((libcache->localfilename == NULL) || (libcache->localkeyfile == NULL)) return FALSE;
 
125
        
 
126
        if (update) {
 
127
                /*Save timestamp*/
 
128
                g_key_file_set_int64(libcache->localkeyfile, MMGUI_LIBPATHS_FILE_ROOT_SECTION, MMGUI_LIBPATHS_FILE_TIMESTAMP, (gint64)libcache->modtime);
 
129
                /*Write to file*/
 
130
                error = NULL;
 
131
                filedata = g_key_file_to_data(libcache->localkeyfile, &datasize, &error);
 
132
                if (filedata != NULL) {
 
133
                        if (!g_file_set_contents(libcache->localfilename, filedata, datasize, &error)) {
 
134
                                g_debug("No data saved to local cache file file: %s", error->message);
 
135
                                g_error_free(error);
 
136
                        }
 
137
                }
 
138
                g_free(filedata);
 
139
        }
 
140
        /*Free resources*/
 
141
        g_free(libcache->localfilename);
 
142
        g_key_file_free(libcache->localkeyfile);
 
143
        
 
144
        return TRUE;
 
145
}
 
146
 
 
147
static gboolean mmgui_libpaths_cache_add_to_local_cache_file(mmgui_libpaths_cache_t libcache, mmgui_libpaths_entry_t cachedlib)
 
148
{
 
149
        if ((libcache == NULL) || (cachedlib == NULL)) return FALSE;
 
150
        
 
151
        if ((libcache->updatelocal) && (libcache->localkeyfile != NULL)) {
 
152
                if (cachedlib->id != NULL) {
 
153
                        /*Library name*/
 
154
                        if (cachedlib->libname != NULL) {
 
155
                                g_key_file_set_string(libcache->localkeyfile, cachedlib->id, MMGUI_LIBPATHS_FILE_NAME, cachedlib->libname);
 
156
                        }
 
157
                        /*Library path*/
 
158
                        if (cachedlib->libpath != NULL) {
 
159
                                g_key_file_set_string(libcache->localkeyfile, cachedlib->id, MMGUI_LIBPATHS_FILE_PATH, cachedlib->libpath);
 
160
                        }
 
161
                }
 
162
        }
 
163
        
 
164
        return TRUE;
 
165
}
 
166
 
 
167
static gboolean mmgui_libpaths_cache_get_from_local_cache_file(mmgui_libpaths_cache_t libcache, mmgui_libpaths_entry_t cachedlib)
 
168
{
 
169
        GError *error;
 
170
        
 
171
        if ((libcache == NULL) || (cachedlib == NULL)) return FALSE;
 
172
        
 
173
        if ((!libcache->updatelocal) && (libcache->localkeyfile != NULL)) {
 
174
                if (cachedlib->id != NULL) {
 
175
                        /*Library name*/
 
176
                        error = NULL;
 
177
                        if (g_key_file_has_key(libcache->localkeyfile, cachedlib->id, MMGUI_LIBPATHS_FILE_NAME, &error)) {
 
178
                                error = NULL;
 
179
                                cachedlib->libname = g_key_file_get_string(libcache->localkeyfile, cachedlib->id, MMGUI_LIBPATHS_FILE_NAME, &error);
 
180
                                if (error != NULL) {
 
181
                                        g_debug("Local cache contain unreadable library name: %s", error->message);
 
182
                                        g_error_free(error);
 
183
                                }
 
184
                        } else {
 
185
                                cachedlib->libname = NULL;
 
186
                                g_debug("Local cache does not contain library name: %s", error->message);
 
187
                                g_error_free(error);
 
188
                        }
 
189
                        /*Library path*/
 
190
                        error = NULL;
 
191
                        if (g_key_file_has_key(libcache->localkeyfile, cachedlib->id, MMGUI_LIBPATHS_FILE_PATH, &error)) {
 
192
                                error = NULL;
 
193
                                cachedlib->libpath = g_key_file_get_string(libcache->localkeyfile, cachedlib->id, MMGUI_LIBPATHS_FILE_PATH, &error);
 
194
                                if (error != NULL) {
 
195
                                        g_debug("Local cache contain unreadable library path: %s", error->message);
 
196
                                        g_error_free(error);
 
197
                                }
 
198
                        } else {
 
199
                                cachedlib->libpath = NULL;
 
200
                                g_debug("Local cache does not contain library path: %s", error->message);
 
201
                                g_error_free(error);
 
202
                        }
 
203
                }
 
204
        }
 
205
        
 
206
        return TRUE;
 
207
}
 
208
 
 
209
static void mmgui_libpaths_cache_destroy_entry(gpointer data)
 
210
{
 
211
        mmgui_libpaths_entry_t cachedlib;
 
212
        
 
213
        cachedlib = (mmgui_libpaths_entry_t)data;
 
214
        
 
215
        if (cachedlib == NULL) return;
 
216
        
 
217
        if (cachedlib->id != NULL) {
 
218
                g_free(cachedlib->id);
 
219
        }
 
220
        if (cachedlib->libname != NULL) {
 
221
                g_free(cachedlib->libname);
 
222
        }
 
223
        if (cachedlib->libpath != NULL) {
 
224
                g_free(cachedlib->libpath);
 
225
        }
 
226
        g_free(cachedlib);
 
227
}
 
228
 
 
229
static gboolean mmgui_libpaths_cache_get_entry(mmgui_libpaths_cache_t libcache, gchar *libpath)
 
230
{
 
231
        mmgui_libpaths_entry_t cachedlib;
 
232
        gchar *libext, *libid;
 
233
        guint pathlen, sym, lnsym, lilen, lnlen;
 
234
        gboolean res;
 
235
        
 
236
        if ((libcache == NULL) || (libpath == NULL)) return FALSE;
 
237
        
 
238
        pathlen = strlen(libpath);
 
239
        
 
240
        if (pathlen == 0) return FALSE;
 
241
        
 
242
        libext = strstr(libpath, MMGUI_LIBPATHS_CACHE_SOEXT);
 
243
        
 
244
        if (libext == NULL) return FALSE;
 
245
        
 
246
        lnsym = 0;
 
247
        lilen = 0;
 
248
        lnlen = 0;
 
249
        
 
250
        for (sym = libext-libpath; sym >= 0; sym--) {
 
251
                if (libpath[sym] == '/') {
 
252
                        lnsym = sym + 1;
 
253
                        lilen = libext - libpath - sym - 1;
 
254
                        lnlen = pathlen - sym - 1;
 
255
                        break;
 
256
                }
 
257
        }
 
258
        
 
259
        if ((lilen == 0) || (lnlen == 0)) return FALSE;
 
260
        
 
261
        /*library identifier*/
 
262
        libid = g_malloc0(lilen+1);
 
263
        strncpy(libid, libpath+lnsym, lilen);
 
264
        /*search in hash table*/
 
265
        cachedlib = (mmgui_libpaths_entry_t)g_hash_table_lookup(libcache->cache, libid);
 
266
        res = FALSE;
 
267
        
 
268
        if (cachedlib != NULL) {
 
269
                if (cachedlib->libname == NULL) {
 
270
                        /*library name*/
 
271
                        cachedlib->libname = g_malloc0(lnlen+1);
 
272
                        strncpy(cachedlib->libname, libpath+lnsym, lnlen);
 
273
                        /*library name found*/
 
274
                        mmgui_libpaths_cache_add_to_local_cache_file(libcache, cachedlib);
 
275
                        g_debug("Library name: %s (%s)\n", cachedlib->libname, libid);
 
276
                } 
 
277
                if (cachedlib->libpath == NULL) {
 
278
                        /*full library path*/
 
279
                        cachedlib->libpath = g_strdup(libpath);
 
280
                        /*library path found*/
 
281
                        mmgui_libpaths_cache_add_to_local_cache_file(libcache, cachedlib);
 
282
                        g_debug("Library path: %s (%s)\n", cachedlib->libpath, libid);
 
283
                }
 
284
                res = TRUE;
 
285
        }
 
286
        
 
287
        g_free(libid);
 
288
        
 
289
        return res;
 
290
}
 
291
 
 
292
static guint mmgui_libpaths_cache_parse_db(mmgui_libpaths_cache_t libcache)
 
293
{
 
294
        guint ptr, start, end, entry, entries;
 
295
        gchar *entryhash, *entryname, *entryext;
 
296
        
 
297
        if (libcache == NULL) return;
 
298
        
 
299
        /*Cache file must be terminated with value 0x00*/
 
300
        if (libcache->mapping[libcache->mapsize-1] != 0x00) {
 
301
                g_debug("Cache file seems to be non-valid\n");
 
302
                return 0;
 
303
        }
 
304
        
 
305
        start = 0;
 
306
        end = libcache->mapsize-1;
 
307
        entries = 0;
 
308
        entry = 0;
 
309
                
 
310
        for (ptr = libcache->mapsize-1; ptr > 0; ptr--) {
 
311
                if (libcache->mapping[ptr] == 0x00) {
 
312
                        /*String separator - value 0x00*/
 
313
                        if ((end - ptr) == 1) {
 
314
                                /*Termination sequence - two values 0x00 0x00*/
 
315
                                if (start < end) {
 
316
                                        if (libcache->mapping[start] == '/') {
 
317
                                                mmgui_libpaths_cache_get_entry(libcache, libcache->mapping+start);
 
318
                                                entries++;
 
319
                                        }
 
320
                                        entry++;
 
321
                                }
 
322
                                break;
 
323
                        } else {
 
324
                                /*Regular cache entry*/
 
325
                                if (start < end) {
 
326
                                        if (libcache->mapping[start] == '/') {
 
327
                                                mmgui_libpaths_cache_get_entry(libcache, libcache->mapping+start);
 
328
                                                entries++;
 
329
                                        }
 
330
                                        entry++;
 
331
                                }
 
332
                                /*Set end pointer to string end*/
 
333
                                end = ptr;
 
334
                        }
 
335
                } else if (isprint(libcache->mapping[ptr])) {
 
336
                        /*Move start pointer because this value is print symbol*/
 
337
                        start = ptr;
 
338
                }
 
339
        }
 
340
        
 
341
        return entries;
 
342
}
 
343
 
 
344
mmgui_libpaths_cache_t mmgui_libpaths_cache_new(gchar *libname, ...)
 
345
{
 
346
        va_list libnames;
 
347
        gchar *currentlib;
 
348
        struct stat statbuf;
 
349
        gboolean localcopy;
 
350
        mmgui_libpaths_cache_t libcache;
 
351
        mmgui_libpaths_entry_t cachedlib;
 
352
                
 
353
        if (libname == NULL) return NULL;
 
354
        
 
355
        libcache = (mmgui_libpaths_cache_t)g_new0(struct _mmgui_libpaths_cache, 1);
 
356
        
 
357
        if (stat(MMGUI_LIBPATHS_CACHE_FILE, &statbuf) == -1) {
 
358
                g_debug("Failed to get library paths cache file size\n");
 
359
                g_free(libcache);
 
360
                return NULL;
 
361
        }
 
362
        
 
363
        libcache->modtime = statbuf.st_mtime;
 
364
        
 
365
        localcopy = mmgui_libpaths_cache_open_local_cache_file(libcache, (guint64)libcache->modtime);
 
366
        
 
367
        if (!localcopy) {
 
368
                /*Open system cache*/
 
369
                libcache->fd = open(MMGUI_LIBPATHS_CACHE_FILE, O_RDONLY);
 
370
                if (libcache->fd == -1) {
 
371
                        g_debug("Failed to open library paths cache file\n");
 
372
                        mmgui_libpaths_cache_close_local_cache_file(libcache, FALSE);
 
373
                        g_free(libcache);
 
374
                        return NULL;
 
375
                }
 
376
                /*Memory mapping size*/
 
377
                libcache->mapsize = (size_t)statbuf.st_size;
 
378
                
 
379
                if (libcache->mapsize == 0) {
 
380
                        g_debug("Failed to map empty library paths cache file\n");
 
381
                        mmgui_libpaths_cache_close_local_cache_file(libcache, FALSE);
 
382
                        close(libcache->fd);
 
383
                        g_free(libcache);
 
384
                        return NULL;
 
385
                }
 
386
                /*Map file into memory*/
 
387
                libcache->mapping = mmap(NULL, libcache->mapsize, PROT_READ, MAP_PRIVATE, libcache->fd, 0);
 
388
                
 
389
                if (libcache->mapping == MAP_FAILED) {
 
390
                        g_debug("Failed to map library paths cache file into memory\n");
 
391
                        mmgui_libpaths_cache_close_local_cache_file(libcache, FALSE);
 
392
                        close(libcache->fd);
 
393
                        g_free(libcache);
 
394
                        return NULL;
 
395
                }
 
396
        }
 
397
        
 
398
        /*When no entry found in cache, form safe name adding .so extension*/
 
399
        libcache->safename = NULL;
 
400
        
 
401
        /*Cache for requested libraries*/
 
402
        libcache->cache = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)mmgui_libpaths_cache_destroy_entry);
 
403
        
 
404
        va_start(libnames, libname);
 
405
        
 
406
        /*Dont forget about first library name*/
 
407
        currentlib = libname;
 
408
        
 
409
        do {
 
410
                /*Allocate structure*/
 
411
                cachedlib = (mmgui_libpaths_entry_t)g_new0(struct _mmgui_libpaths_entry, 1);
 
412
                cachedlib->id = g_strdup(currentlib);
 
413
                cachedlib->libname = NULL;
 
414
                cachedlib->libpath = NULL;
 
415
                g_hash_table_insert(libcache->cache, cachedlib->id, cachedlib);
 
416
                /*If available, get from local cache*/
 
417
                if (localcopy) {
 
418
                        mmgui_libpaths_cache_get_from_local_cache_file(libcache, cachedlib);
 
419
                }
 
420
                /*Next library name*/
 
421
                currentlib = va_arg(libnames, gchar *);
 
422
        } while (currentlib != NULL);
 
423
        
 
424
        va_end(libnames);
 
425
        
 
426
        if (!localcopy) {
 
427
                /*Parse system database*/
 
428
                mmgui_libpaths_cache_parse_db(libcache);
 
429
                /*Save local cache*/
 
430
                mmgui_libpaths_cache_close_local_cache_file(libcache, TRUE);
 
431
        } else {
 
432
                /*Close used cache file*/
 
433
                mmgui_libpaths_cache_close_local_cache_file(libcache, FALSE);
 
434
        }
 
435
        
 
436
        return libcache;
 
437
}
 
438
 
 
439
void mmgui_libpaths_cache_close(mmgui_libpaths_cache_t libcache)
 
440
{
 
441
        if (libcache == NULL) return;
 
442
        
 
443
        if (libcache->safename != NULL) {
 
444
                g_free(libcache->safename);
 
445
        }
 
446
        
 
447
        g_hash_table_destroy(libcache->cache);
 
448
        
 
449
        munmap(libcache->mapping, libcache->mapsize);
 
450
        
 
451
        g_free(libcache);
 
452
}
 
453
 
 
454
gchar *mmgui_libpaths_cache_get_library_name(mmgui_libpaths_cache_t libcache, gchar *libname)
 
455
{
 
456
        mmgui_libpaths_entry_t cachedlib;
 
457
        
 
458
        if ((libcache == NULL) || (libname == NULL)) return NULL;
 
459
        
 
460
        cachedlib = (mmgui_libpaths_entry_t)g_hash_table_lookup(libcache->cache, libname);
 
461
        
 
462
        if (cachedlib != NULL) {
 
463
                if (cachedlib->libname != NULL) {
 
464
                        /*Cached library name*/
 
465
                        return cachedlib->libname;
 
466
                } else {
 
467
                        /*Safe library name*/
 
468
                        if (libcache->safename != NULL) {
 
469
                                g_free(libcache->safename);
 
470
                        }
 
471
                        libcache->safename = g_strdup_printf(MMGUI_LIBPATHS_CACHE_LIB_TEMP, libname);
 
472
                        return libcache->safename;
 
473
                }
 
474
        } else {
 
475
                /*Safe library name*/
 
476
                if (libcache->safename != NULL) {
 
477
                        g_free(libcache->safename);
 
478
                }
 
479
                libcache->safename = g_strdup_printf(MMGUI_LIBPATHS_CACHE_LIB_TEMP, libname);
 
480
                return libcache->safename;
 
481
        }
 
482
}
 
483
 
 
484
gchar *mmgui_libpaths_cache_get_library_path(mmgui_libpaths_cache_t libcache, gchar *libname)
 
485
{
 
486
        mmgui_libpaths_entry_t cachedlib;
 
487
        
 
488
        if ((libcache == NULL) || (libname == NULL)) return NULL;
 
489
        
 
490
        cachedlib = (mmgui_libpaths_entry_t)g_hash_table_lookup(libcache->cache, libname);
 
491
        
 
492
        if (cachedlib != NULL) {
 
493
                if (cachedlib->libpath != NULL) {
 
494
                        /*Cached library path*/
 
495
                        return cachedlib->libpath;
 
496
                } else {
 
497
                        /*Safe library path*/
 
498
                        if (libcache->safename != NULL) {
 
499
                                g_free(libcache->safename);
 
500
                        }
 
501
                        libcache->safename = g_strdup_printf(MMGUI_LIBPATHS_CACHE_PATH_TEMP, libname);
 
502
                        return libcache->safename;
 
503
                }
 
504
        } else {
 
505
                /*Safe library path*/
 
506
                if (libcache->safename != NULL) {
 
507
                        g_free(libcache->safename);
 
508
                }
 
509
                libcache->safename = g_strdup_printf(MMGUI_LIBPATHS_CACHE_PATH_TEMP, libname);
 
510
                return libcache->safename;
 
511
        }
 
512
}