Split up functionality of plugin in separate modules
[mtetherd] / util.c
diff --git a/util.c b/util.c
new file mode 100644 (file)
index 0000000..cc3966d
--- /dev/null
+++ b/util.c
@@ -0,0 +1,72 @@
+/*
+  mtetherd
+  (c) 2010 Gregor Riepl <onitake@gmail.com>
+  
+  Tethering utility for Maemo
+  
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+  
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+  
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include <glib.h>
+
+static const char *MODULE_LIST = "/proc/modules";
+
+gboolean mtetherd_scan_modules(const char *module) {
+       g_message("Scanning %s", MODULE_LIST);
+
+       FILE *fp = fopen(MODULE_LIST, "r");
+       if (!fp) {
+               return FALSE;
+       }
+       gboolean found = FALSE;
+       char *line = NULL;
+       while (!found && getline(&line, NULL, fp) != -1) {
+               g_debug("Checking if '%s' starts with %s...", line, module);
+               if (strcmp(line, module, strlen(module)) == 0) {
+                       g_message("Found %s", module);
+                       found = TRUE;
+               }
+               free(line);
+               line = NULL;
+       }
+       fclose(fp);
+       return found;
+}
+
+gboolean mtetherd_launch_script(const char *command[]) {
+/*
+       const char *arg;
+       if (enable) {
+               arg = SBIN_DIR "mtetherd-usbnet-enable.sh";
+       } else {
+               arg = SBIN_DIR "mtetherd-usbnet-disable.sh";
+       }
+       g_debug("Launching %s", arg);
+       const char *command[] = { "/usr/bin/sudo", arg, NULL };
+*/
+       pid_t pid = fork();
+       if (pid == 0) {
+               if (execv(command[0], (char **const) command) == -1) {
+                       exit(1);
+               }
+       } else if (pid == -1) {
+               // error forking
+               return FALSE;
+       }
+       // launch ok
+       return TRUE;
+}
+