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

« back to all changes in this revision

Viewing changes to src/modules/pppd245.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
 *      pppd245.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
 
 
21
#include <glib.h>
 
22
#include <glib/gi18n.h>
 
23
#include <gio/gio.h>
 
24
#include <gmodule.h>
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <fcntl.h>
 
28
#include <unistd.h>
 
29
#include <sys/mman.h>
 
30
#include <sys/types.h>
 
31
#include <sys/stat.h>
 
32
#include <string.h>
 
33
#include <net/if.h>
 
34
 
 
35
#include "../mmguicore.h"
 
36
 
 
37
#define MMGUI_MODULE_SERVICE_NAME  "/usr/sbin/pppd"
 
38
#define MMGUI_MODULE_IDENTIFIER    245
 
39
#define MMGUI_MODULE_DESCRIPTION   "pppd >= 2.4.5"
 
40
 
 
41
//Internal definitions
 
42
#define MODULE_INT_PPPD_DB_FILE_PATH_1               "/var/run/pppd2.tdb"
 
43
#define MODULE_INT_PPPD_DB_FILE_PATH_2               "/var/run/ppp/pppd2.tdb"
 
44
#define MODULE_INT_PPPD_LOCK_FILE_PATH               "/var/run/%s.pid"
 
45
#define MODULE_INT_PPPD_DB_FILE_START_PARAMETER      "ORIG_UID="
 
46
#define MODULE_INT_PPPD_DB_FILE_END_PARAMETER        "USEPEERDNS="
 
47
#define MODULE_INT_PPPD_DB_FILE_DEVICE_PARAMETER     "DEVICE="
 
48
#define MODULE_INT_PPPD_DB_FILE_INTERFACE_PARAMETER  "IFNAME="
 
49
#define MODULE_INT_PPPD_DB_FILE_PID_PARAMETER        "PPPD_PID="
 
50
#define MODULE_INT_PPPD_SYSFS_DEVICE_PATH            "/sys/dev/char/%u:%u"
 
51
#define MODULE_INT_PPPD_SYSFS_START_PARAMETER        "devices/pci"
 
52
#define MODULE_INT_PPPD_SYSFS_END_PARAMETER          '/'
 
53
 
 
54
//Private module variables
 
55
struct _mmguimoduledata {
 
56
        //Device serial
 
57
        gchar devserial[32];
 
58
        //Connected device information
 
59
        gchar conndevice[256];
 
60
        gchar connserial[32];
 
61
        gchar connifname[IFNAMSIZ];
 
62
        //pppd service pid
 
63
        pid_t pid;
 
64
        //Last error message
 
65
        gchar *errormessage;
 
66
};
 
67
 
 
68
typedef struct _mmguimoduledata *moduledata_t;
 
69
 
 
70
 
 
71
static void mmgui_module_handle_error_message(mmguicore_t mmguicore, gchar *message)
 
72
{
 
73
        moduledata_t moduledata;
 
74
        
 
75
        if (mmguicore == NULL) return;
 
76
        
 
77
        moduledata = (moduledata_t)mmguicore->cmoduledata;
 
78
        
 
79
        if (moduledata == NULL) return;
 
80
        
 
81
        if (moduledata->errormessage != NULL) {
 
82
                g_free(moduledata->errormessage);
 
83
        }
 
84
        
 
85
        if (message != NULL) {
 
86
                moduledata->errormessage = g_strdup(message);
 
87
        } else {
 
88
                moduledata->errormessage = g_strdup("Unknown error");
 
89
        }
 
90
        
 
91
        g_warning("%s: %s", MMGUI_MODULE_DESCRIPTION, moduledata->errormessage);
 
92
}
 
93
 
 
94
static gchar *mmgui_module_pppd_strrstr(gchar *source, gsize sourcelen, gchar *entry, gsize entrylen)
 
95
{
 
96
        gboolean found;
 
97
        gchar *sourceptr, *curptr;
 
98
        gsize ind;
 
99
        
 
100
        if ((source == NULL) || (entry == NULL)) return NULL;
 
101
        if ((sourcelen == 0) || (entrylen == 0)) return source;
 
102
        
 
103
        sourceptr = source + sourcelen - entrylen;
 
104
        
 
105
        for (curptr=sourceptr; curptr>=source; curptr--) {
 
106
                found = TRUE;
 
107
                for (ind=0; ind<entrylen; ind++) {
 
108
                        if (curptr[ind] != entry[ind]) {
 
109
                                found = FALSE;
 
110
                                break;
 
111
                        }
 
112
                }
 
113
                
 
114
                if (found) {
 
115
                        return curptr;
 
116
                }
 
117
        }
 
118
        
 
119
        return NULL;
 
120
}
 
121
 
 
122
static gchar *mmgui_module_pppd_get_config_fragment(gchar *string, gchar *parameter, gchar *buffer, gsize bufsize)
 
123
{
 
124
        gchar *segend, *segstart;
 
125
        gsize datalen, paramlen;
 
126
        
 
127
        if ((string == NULL) || (parameter == NULL) || (buffer == NULL) || (bufsize == 0)) return NULL;
 
128
        
 
129
        segstart = strstr(string, parameter);
 
130
        
 
131
        if (segstart == NULL) return NULL;
 
132
        
 
133
        segend = strchr(segstart, ';');
 
134
        
 
135
        if (segend == NULL) return NULL;
 
136
        
 
137
        paramlen = strlen(parameter);
 
138
        
 
139
        datalen = segend - segstart - paramlen;
 
140
        
 
141
        if (datalen > (bufsize - 1)) datalen = bufsize - 1;
 
142
        
 
143
        memcpy(buffer, segstart+paramlen, datalen);
 
144
        
 
145
        buffer[datalen] = '\0';
 
146
        
 
147
        return buffer;
 
148
}
 
149
 
 
150
static gchar *mmgui_module_pppd_get_config_string(gpointer dbmapping, gsize dbsize)
 
151
{
 
152
        gchar *segend, *segstart;
 
153
        gchar *result;
 
154
        
 
155
        if ((dbmapping == NULL) || (dbsize == 0)) return NULL;
 
156
        
 
157
        segend = mmgui_module_pppd_strrstr(dbmapping, dbsize, MODULE_INT_PPPD_DB_FILE_END_PARAMETER, strlen(MODULE_INT_PPPD_DB_FILE_END_PARAMETER));
 
158
        
 
159
        if ((segend == NULL) || ((segend != NULL) && ((*segend) >= dbsize))) return NULL;
 
160
        
 
161
        segstart = mmgui_module_pppd_strrstr(dbmapping, dbsize-(*segend), MODULE_INT_PPPD_DB_FILE_START_PARAMETER, strlen(MODULE_INT_PPPD_DB_FILE_START_PARAMETER));
 
162
        
 
163
        if ((segstart == NULL) || ((segstart != NULL) && (segend <= segstart))) return NULL;
 
164
        
 
165
        result = g_malloc0(segend-segstart+1);
 
166
        
 
167
        if (result == NULL) return NULL;
 
168
        
 
169
        memcpy(result, segstart, segend-segstart);
 
170
        
 
171
        return result;
 
172
}
 
173
 
 
174
static gchar *mmgui_module_pppd_get_device_serial(gchar *string, gchar *buffer, gsize bufsize)
 
175
{
 
176
        gchar *segend, *segstart;
 
177
        gsize datalen, paramlen;
 
178
        
 
179
        if ((string == NULL) || (buffer == NULL) || (bufsize == 0)) return NULL;
 
180
        
 
181
        paramlen = strlen(MODULE_INT_PPPD_SYSFS_START_PARAMETER);
 
182
        
 
183
        segstart = strstr(string, MODULE_INT_PPPD_SYSFS_START_PARAMETER);
 
184
        
 
185
        if (segstart == NULL) return NULL;
 
186
        
 
187
        segstart = strchr(segstart + paramlen, MODULE_INT_PPPD_SYSFS_END_PARAMETER);
 
188
        
 
189
        if (segstart == NULL) return NULL;
 
190
        
 
191
        segend = strchr(segstart+paramlen, MODULE_INT_PPPD_SYSFS_END_PARAMETER);
 
192
        
 
193
        if (segend == NULL) return NULL;
 
194
        
 
195
        datalen = segend - segstart - 1;
 
196
        
 
197
        if (datalen > (bufsize - 1)) datalen = bufsize - 1;
 
198
        
 
199
        memcpy(buffer, segstart + 1, datalen);
 
200
        
 
201
        buffer[datalen] = '\0';
 
202
        
 
203
        return buffer;
 
204
}
 
205
 
 
206
static gboolean mmgui_module_pppd_get_daemon_running(moduledata_t moduledata)
 
207
{
 
208
        gchar pidfilepath[32], pidfiledata[32];
 
209
        gint pidfilefd, status;
 
210
        struct flock pidfilelock;
 
211
        
 
212
        if (moduledata == NULL) return FALSE;
 
213
        if (moduledata->connifname[0] == '\0') return FALSE;
 
214
        
 
215
        memset(pidfilepath, 0, sizeof(pidfilepath));
 
216
        memset(pidfiledata, 0, sizeof(pidfiledata));
 
217
        
 
218
        snprintf(pidfilepath, sizeof(pidfilepath), MODULE_INT_PPPD_LOCK_FILE_PATH, moduledata->connifname);
 
219
        
 
220
        pidfilefd = open(pidfilepath, O_RDONLY);
 
221
        
 
222
        if (pidfilefd == -1) return FALSE;
 
223
        
 
224
        status = read(pidfilefd, pidfiledata, sizeof(pidfiledata));
 
225
        
 
226
        close(pidfilefd);
 
227
        
 
228
        if ((status != -1) && ((pid_t)atoi(pidfiledata) == moduledata->pid)) {
 
229
                return TRUE;
 
230
        } else {
 
231
                return FALSE;
 
232
        }
 
233
}
 
234
 
 
235
static void mmgui_module_pppd_set_connection_status(gpointer mmguicore, gboolean connected)
 
236
{
 
237
        mmguicore_t mmguicorelc;
 
238
        moduledata_t moduledata;
 
239
        
 
240
        if (mmguicore == NULL) return;
 
241
        mmguicorelc = (mmguicore_t)mmguicore;
 
242
        
 
243
        if (mmguicorelc->moduledata == NULL) return;
 
244
        moduledata = (moduledata_t)mmguicorelc->cmoduledata;
 
245
        
 
246
        if (mmguicorelc->device == NULL) return;
 
247
        
 
248
        if (connected) {
 
249
                memset(mmguicorelc->device->interface, 0, IFNAMSIZ);
 
250
                strncpy(mmguicorelc->device->interface, moduledata->connifname, IFNAMSIZ);
 
251
                mmguicorelc->device->connected = TRUE;
 
252
        } else {
 
253
                memset(mmguicorelc->device->interface, 0, IFNAMSIZ);
 
254
                mmguicorelc->device->connected = FALSE;
 
255
        }
 
256
}
 
257
 
 
258
G_MODULE_EXPORT gboolean mmgui_module_init(mmguimodule_t module)
 
259
{
 
260
        if (module == NULL) return FALSE;
 
261
        
 
262
        module->type = MMGUI_MODULE_TYPE_CONNECTION_MANGER;
 
263
        module->requirement = MMGUI_MODULE_REQUIREMENT_FILE;
 
264
        module->priority = MMGUI_MODULE_PRIORITY_LOW;
 
265
        module->identifier = MMGUI_MODULE_IDENTIFIER;
 
266
        module->functions = MMGUI_MODULE_FUNCTION_BASIC;
 
267
        g_snprintf(module->servicename, sizeof(module->servicename), MMGUI_MODULE_SERVICE_NAME);
 
268
        g_snprintf(module->description, sizeof(module->description), MMGUI_MODULE_DESCRIPTION);
 
269
        
 
270
        return TRUE;
 
271
}
 
272
 
 
273
G_MODULE_EXPORT gboolean mmgui_module_connection_open(gpointer mmguicore)
 
274
{
 
275
        mmguicore_t mmguicorelc;
 
276
        moduledata_t *moduledata;
 
277
        
 
278
        if (mmguicore == NULL) return FALSE;
 
279
        
 
280
        mmguicorelc = (mmguicore_t)mmguicore;
 
281
        
 
282
        moduledata = (moduledata_t *)&mmguicorelc->cmoduledata;
 
283
        
 
284
        (*moduledata) = g_new0(struct _mmguimoduledata, 1);
 
285
        
 
286
        (*moduledata)->errormessage = NULL;
 
287
        
 
288
        return TRUE;
 
289
}
 
290
 
 
291
G_MODULE_EXPORT gboolean mmgui_module_connection_close(gpointer mmguicore)
 
292
{
 
293
        mmguicore_t mmguicorelc;
 
294
        moduledata_t moduledata;
 
295
                
 
296
        if (mmguicore == NULL) return FALSE;
 
297
        mmguicorelc = (mmguicore_t)mmguicore;
 
298
        moduledata = (moduledata_t)(mmguicorelc->cmoduledata);
 
299
        
 
300
        if (moduledata != NULL) {
 
301
                if (moduledata->errormessage != NULL) {
 
302
                        g_free(moduledata->errormessage);
 
303
                }
 
304
                
 
305
                g_free(moduledata);
 
306
        }
 
307
        
 
308
        return TRUE;
 
309
}
 
310
 
 
311
G_MODULE_EXPORT gchar *mmgui_module_connection_last_error(gpointer mmguicore)
 
312
{
 
313
        mmguicore_t mmguicorelc;
 
314
        moduledata_t moduledata;
 
315
        
 
316
        if (mmguicore == NULL) return NULL;
 
317
        
 
318
        mmguicorelc = (mmguicore_t)mmguicore;
 
319
        moduledata = (moduledata_t)(mmguicorelc->cmoduledata);
 
320
        
 
321
        return moduledata->errormessage;
 
322
}
 
323
 
 
324
G_MODULE_EXPORT gboolean mmgui_module_device_connection_open(gpointer mmguicore, mmguidevice_t device)
 
325
{
 
326
        mmguicore_t mmguicorelc;
 
327
        moduledata_t moduledata;
 
328
                
 
329
        if ((mmguicore == NULL) || (device == NULL)) return FALSE;
 
330
        mmguicorelc = (mmguicore_t)mmguicore;
 
331
        
 
332
        if (mmguicorelc->cmoduledata == NULL) return FALSE;
 
333
        moduledata = (moduledata_t)mmguicorelc->cmoduledata;
 
334
        
 
335
        if (device->sysfspath == NULL) {
 
336
                mmgui_module_handle_error_message(mmguicorelc, "Device data not available");
 
337
                return FALSE;
 
338
        }
 
339
        
 
340
        if (mmgui_module_pppd_get_device_serial(device->sysfspath, moduledata->devserial, sizeof(moduledata->devserial)) == NULL) {
 
341
                mmgui_module_handle_error_message(mmguicorelc, "Device serial not available");
 
342
                return FALSE;
 
343
        }
 
344
        
 
345
        return TRUE;
 
346
}
 
347
 
 
348
G_MODULE_EXPORT gboolean mmgui_module_device_connection_close(gpointer mmguicore)
 
349
{
 
350
        mmguicore_t mmguicorelc;
 
351
        moduledata_t moduledata;
 
352
                                        
 
353
        if (mmguicore == NULL) return FALSE;
 
354
        mmguicorelc = (mmguicore_t)mmguicore;
 
355
        
 
356
        if (mmguicorelc->moduledata == NULL) return FALSE;
 
357
        moduledata = (moduledata_t)mmguicorelc->cmoduledata;
 
358
        
 
359
        memset(moduledata->devserial, 0, sizeof(moduledata->devserial));
 
360
                
 
361
        return TRUE;
 
362
}
 
363
 
 
364
G_MODULE_EXPORT gboolean mmgui_module_device_connection_status(gpointer mmguicore)
 
365
{
 
366
        mmguicore_t mmguicorelc;
 
367
        moduledata_t moduledata;
 
368
        gint dbfd, state;
 
369
        struct stat statbuf;
 
370
        gpointer dbmapping;
 
371
        gchar *pppdconfig;
 
372
        gchar intbuf[16], sysfspath[128], sysfslink[128];
 
373
        gchar *parameter;
 
374
        gint status, devmajor, devminor;
 
375
        
 
376
        if (mmguicore == NULL) return FALSE;
 
377
        mmguicorelc = (mmguicore_t)mmguicore;
 
378
        
 
379
        if (mmguicorelc->moduledata == NULL) return FALSE;
 
380
        moduledata = (moduledata_t)mmguicorelc->cmoduledata;
 
381
        
 
382
        if (mmguicorelc->device == NULL) return FALSE;
 
383
        if (moduledata->devserial[0] == '\0') return FALSE;
 
384
        
 
385
        /*Usual PPPD database path*/
 
386
        dbfd = open(MODULE_INT_PPPD_DB_FILE_PATH_1, O_RDONLY);
 
387
        /*Try special path first*/
 
388
        if (dbfd == -1) {
 
389
                /*Special PPPD database path (Fedora uses it)*/
 
390
                dbfd = open(MODULE_INT_PPPD_DB_FILE_PATH_2, O_RDONLY);
 
391
                /*No database found*/
 
392
                if (dbfd == -1) {
 
393
                        mmgui_module_pppd_set_connection_status(mmguicore, FALSE);
 
394
                        g_debug("Failed to open PPPD database file\n");
 
395
                        return TRUE;
 
396
                }
 
397
        }
 
398
        
 
399
        state = fstat(dbfd, &statbuf);
 
400
        
 
401
        if (state == -1) {
 
402
                mmgui_module_pppd_set_connection_status(mmguicore, FALSE);
 
403
                g_debug("Failed to get PPPD database file size\n");
 
404
                close(dbfd);
 
405
                return TRUE;
 
406
        }
 
407
        
 
408
        dbmapping = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, dbfd, 0);
 
409
        
 
410
        if(dbmapping == (void *)-1) {
 
411
                mmgui_module_pppd_set_connection_status(mmguicore, FALSE);
 
412
                mmgui_module_handle_error_message(mmguicorelc, "Failed to map PPPD database file into memory");
 
413
                close(dbfd);
 
414
                return TRUE;
 
415
        }
 
416
        
 
417
        pppdconfig = mmgui_module_pppd_get_config_string(dbmapping, statbuf.st_size);
 
418
        
 
419
        if (pppdconfig == NULL) {
 
420
                mmgui_module_pppd_set_connection_status(mmguicore, FALSE);
 
421
                mmgui_module_handle_error_message(mmguicorelc, "Failed to get config");
 
422
                munmap(dbmapping, statbuf.st_size);
 
423
                close(dbfd);
 
424
                return TRUE;
 
425
        }
 
426
        
 
427
        munmap(dbmapping, statbuf.st_size);
 
428
        close(dbfd);
 
429
        
 
430
        parameter = mmgui_module_pppd_get_config_fragment(pppdconfig, MODULE_INT_PPPD_DB_FILE_DEVICE_PARAMETER, moduledata->conndevice, sizeof(moduledata->conndevice));
 
431
        if (parameter == NULL) {
 
432
                mmgui_module_pppd_set_connection_status(mmguicore, FALSE);
 
433
                g_debug("Device entry not found in PPPD database\n");
 
434
                g_free(pppdconfig);
 
435
                return TRUE;
 
436
        }
 
437
        
 
438
        parameter = mmgui_module_pppd_get_config_fragment(pppdconfig, MODULE_INT_PPPD_DB_FILE_INTERFACE_PARAMETER, moduledata->connifname, sizeof(moduledata->connifname));
 
439
        if (parameter == NULL) {
 
440
                mmgui_module_pppd_set_connection_status(mmguicore, FALSE);
 
441
                g_debug("Interface entry not found in PPPD database\n");
 
442
                g_free(pppdconfig);
 
443
                return TRUE;
 
444
        }
 
445
        
 
446
        parameter = mmgui_module_pppd_get_config_fragment(pppdconfig, MODULE_INT_PPPD_DB_FILE_PID_PARAMETER, intbuf, sizeof(intbuf));
 
447
        if (parameter == NULL) {
 
448
                mmgui_module_pppd_set_connection_status(mmguicore, FALSE);
 
449
                g_debug("PPPD pid not found in PPPD database\n");
 
450
                g_free(pppdconfig);
 
451
                return TRUE;
 
452
        } else {
 
453
                moduledata->pid = (pid_t)atoi(parameter);
 
454
        }
 
455
        
 
456
        g_free(pppdconfig);
 
457
        
 
458
        if (!mmgui_module_pppd_get_daemon_running(moduledata)) {
 
459
                mmgui_module_pppd_set_connection_status(mmguicore, FALSE);
 
460
                g_debug("PPPD daemon is not running\n");
 
461
                return TRUE;
 
462
        }
 
463
        
 
464
        status = stat(moduledata->conndevice, &statbuf);
 
465
        
 
466
        if ((status == -1) || ((status == 0) && (!S_ISCHR(statbuf.st_mode)))) {
 
467
                mmgui_module_pppd_set_connection_status(mmguicore, FALSE);
 
468
                g_debug("Device not suitable\n");
 
469
                return TRUE;
 
470
        }
 
471
        
 
472
        devmajor = major(statbuf.st_rdev);
 
473
        devminor = minor(statbuf.st_rdev);
 
474
        
 
475
        memset(sysfspath, 0, sizeof(sysfspath));
 
476
        memset(sysfslink, 0, sizeof(sysfslink));
 
477
        
 
478
        snprintf(sysfspath, sizeof(sysfspath), MODULE_INT_PPPD_SYSFS_DEVICE_PATH, devmajor, devminor);
 
479
        
 
480
        status = readlink((const char *)sysfspath, sysfslink, sizeof(sysfslink));
 
481
        
 
482
        if (status == -1) {
 
483
                mmgui_module_pppd_set_connection_status(mmguicore, FALSE);
 
484
                mmgui_module_handle_error_message(mmguicorelc, "Device sysfs path not available");
 
485
                return TRUE;
 
486
        }
 
487
        
 
488
        sysfslink[status] = '\0';
 
489
        
 
490
        if (mmgui_module_pppd_get_device_serial(sysfslink, moduledata->connserial, sizeof(moduledata->connserial)) == NULL) {
 
491
                mmgui_module_pppd_set_connection_status(mmguicore, FALSE);
 
492
                mmgui_module_handle_error_message(mmguicorelc, "Device serial not available");
 
493
                return TRUE;
 
494
        }
 
495
        
 
496
        if (g_str_equal(moduledata->connserial, moduledata->devserial)) {
 
497
                mmgui_module_pppd_set_connection_status(mmguicore, TRUE);
 
498
        } else {
 
499
                mmgui_module_pppd_set_connection_status(mmguicore, FALSE);
 
500
        }
 
501
        
 
502
        return TRUE;
 
503
}
 
504
 
 
505
G_MODULE_EXPORT guint64 mmgui_module_device_connection_timestamp(gpointer mmguicore)
 
506
{
 
507
        mmguicore_t mmguicorelc;
 
508
        moduledata_t moduledata;
 
509
        gchar lockfilepath[128];
 
510
        guint64 timestamp;
 
511
        struct stat statbuf;
 
512
        
 
513
        if (mmguicore == NULL) return FALSE;
 
514
        mmguicorelc = (mmguicore_t)mmguicore;
 
515
        
 
516
        if (mmguicorelc->moduledata == NULL) return FALSE;
 
517
        moduledata = (moduledata_t)mmguicorelc->cmoduledata;
 
518
        
 
519
        if (mmguicorelc->device == NULL) return FALSE;
 
520
        
 
521
        /*Get current timestamp*/
 
522
        timestamp = (guint64)time(NULL);
 
523
        
 
524
        /*Form lock file path*/
 
525
        memset(lockfilepath, 0, sizeof(lockfilepath));
 
526
        g_snprintf(lockfilepath, sizeof(lockfilepath), MODULE_INT_PPPD_LOCK_FILE_PATH, mmguicorelc->device->interface);
 
527
        /*Get lock file modification timestamp*/
 
528
        if (stat(lockfilepath, &statbuf) == 0) {
 
529
                timestamp = (guint64)statbuf.st_mtime;
 
530
        }
 
531
        
 
532
        return timestamp;
 
533
}
 
534
 
 
535
G_MODULE_EXPORT gboolean mmgui_module_device_connection_disconnect(gpointer mmguicore)
 
536
{
 
537
        mmguicore_t mmguicorelc;
 
538
        moduledata_t moduledata;
 
539
        GError *error;
 
540
        gchar *stderrdata = NULL;
 
541
        gint exitstatus = 0;
 
542
        gchar *argv[3] = {"/sbin/ifdown", NULL, NULL};
 
543
        
 
544
        if (mmguicore == NULL) return FALSE;
 
545
        mmguicorelc = (mmguicore_t)mmguicore;
 
546
        
 
547
        if (mmguicorelc->moduledata == NULL) return FALSE;
 
548
        moduledata = (moduledata_t)mmguicorelc->cmoduledata;
 
549
        
 
550
        if (mmguicorelc->device == NULL) return FALSE;
 
551
        if (moduledata->devserial[0] == '\0') return FALSE;
 
552
        
 
553
        //If device already disconnected, return TRUE
 
554
        if (!mmguicorelc->device->connected) return TRUE;
 
555
        
 
556
        error = NULL;
 
557
        argv[1] = mmguicorelc->device->interface;
 
558
        
 
559
        if(g_spawn_sync(NULL, argv, NULL, G_SPAWN_STDOUT_TO_DEV_NULL, NULL, NULL, NULL, &stderrdata, &exitstatus, &error)) {
 
560
                //Disconnected - update device state
 
561
                memset(mmguicorelc->device->interface, 0, sizeof(mmguicorelc->device->interface));
 
562
                mmguicorelc->device->connected = FALSE;
 
563
                return TRUE;
 
564
        } else {
 
565
                //Failed to disconnect
 
566
                if (error != NULL) {
 
567
                        mmgui_module_handle_error_message(mmguicorelc, error->message);
 
568
                        g_error_free(error);
 
569
                } else if (stderrdata != NULL) {
 
570
                        mmgui_module_handle_error_message(mmguicorelc, stderrdata);
 
571
                        g_free(stderrdata);
 
572
                }
 
573
                return FALSE;
 
574
        }
 
575
}