cc3966d84d2e73db87ed2b2fdd49c22658320d72
[mtetherd] / util.c
1 /*
2   mtetherd
3   (c) 2010 Gregor Riepl <onitake@gmail.com>
4   
5   Tethering utility for Maemo
6   
7   This program is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11   
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16   
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <glib.h>
24
25 static const char *MODULE_LIST = "/proc/modules";
26
27 gboolean mtetherd_scan_modules(const char *module) {
28         g_message("Scanning %s", MODULE_LIST);
29
30         FILE *fp = fopen(MODULE_LIST, "r");
31         if (!fp) {
32                 return FALSE;
33         }
34         gboolean found = FALSE;
35         char *line = NULL;
36         while (!found && getline(&line, NULL, fp) != -1) {
37                 g_debug("Checking if '%s' starts with %s...", line, module);
38                 if (strcmp(line, module, strlen(module)) == 0) {
39                         g_message("Found %s", module);
40                         found = TRUE;
41                 }
42                 free(line);
43                 line = NULL;
44         }
45         fclose(fp);
46         return found;
47 }
48
49 gboolean mtetherd_launch_script(const char *command[]) {
50 /*
51         const char *arg;
52         if (enable) {
53                 arg = SBIN_DIR "mtetherd-usbnet-enable.sh";
54         } else {
55                 arg = SBIN_DIR "mtetherd-usbnet-disable.sh";
56         }
57         g_debug("Launching %s", arg);
58         const char *command[] = { "/usr/bin/sudo", arg, NULL };
59 */
60         pid_t pid = fork();
61         if (pid == 0) {
62                 if (execv(command[0], (char **const) command) == -1) {
63                         exit(1);
64                 }
65         } else if (pid == -1) {
66                 // error forking
67                 return FALSE;
68         }
69         // launch ok
70         return TRUE;
71 }
72