gnokii-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/9] Move device_script into separate file


From: Ladislav Michl
Subject: [PATCH 1/9] Move device_script into separate file
Date: Sat, 25 Jan 2020 10:44:04 +0100

---
 common/Makefile.am        |  1 +
 common/device.c           | 59 ++----------------------------------
 common/forkscript.c       | 63 +++++++++++++++++++++++++++++++++++++++
 include/gnokii-internal.h |  2 ++
 4 files changed, 68 insertions(+), 57 deletions(-)
 create mode 100644 common/forkscript.c

diff --git a/common/Makefile.am b/common/Makefile.am
index 5e6be928..a762ada7 100644
--- a/common/Makefile.am
+++ b/common/Makefile.am
@@ -29,6 +29,7 @@ libgnokii_la_SOURCES = \
        gsm-statemachine.c \
        cfgreader.c \
        device.c \
+       forkscript.c \
        vcard.c \
        vcal.c \
        gnvcal.c \
diff --git a/common/device.c b/common/device.c
index 68257955..9b895708 100644
--- a/common/device.c
+++ b/common/device.c
@@ -27,66 +27,11 @@
 #include "devices/dku2libusb.h"
 #include "devices/socketphonet.h"
 
-#include <errno.h>
-#include <sys/wait.h>
-
 GNOKII_API int device_getfd(struct gn_statemachine *state)
 {
        return state->device.fd;
 }
 
-/* Script handling: */
-static void device_script_cfgfunc(const char *section, const char *key, const 
char *value)
-{
-       setenv(key, value, 1); /* errors ignored */
-}
-
-int device_script(int fd, const char *section, struct gn_statemachine *state)
-{
-       pid_t pid;
-       const char *scriptname;
-       int status;
-
-       if (!strcmp(section, "connect_script"))
-               scriptname = state->config.connect_script;
-       else
-               scriptname = state->config.disconnect_script;
-       if (scriptname[0] == '\0')
-               return 0;
-
-       errno = 0;
-       switch ((pid = fork())) {
-       case -1:
-               fprintf(stderr, _("device_script(\"%s\"): fork() failure: 
%s!\n"), scriptname, strerror(errno));
-               return -1;
-
-       case 0: /* child */
-               cfg_foreach(section, device_script_cfgfunc);
-               errno = 0;
-               if (dup2(fd, 0) != 0 || dup2(fd, 1) != 1 || close(fd)) {
-                       fprintf(stderr, _("device_script(\"%s\"): file 
descriptor preparation failure: %s\n"), scriptname, strerror(errno));
-                       _exit(-1);
-               }
-               /* FIXME: close all open descriptors - how to track them?
-                */
-               execl("/bin/sh", "sh", "-c", scriptname, NULL);
-               fprintf(stderr, _("device_script(\"%s\"): script execution 
failure: %s\n"), scriptname, strerror(errno));
-               _exit(-1);
-               /* NOTREACHED */
-
-       default:
-               if (pid == waitpid(pid, &status, 0 /* options */) && 
WIFEXITED(status) && !WEXITSTATUS(status))
-                       return 0;
-               fprintf(stderr, _("device_script(\"%s\"): child script 
execution failure: %s, exit code=%d\n"), scriptname,
-                       (WIFEXITED(status) ? _("normal exit") : _("abnormal 
exit")),
-                       (WIFEXITED(status) ? WEXITSTATUS(status) : -1));
-               errno = EIO;
-               return -1;
-
-       }
-       /* NOTREACHED */
-}
-
 int device_open(const char *file, int with_odd_parity, int with_async,
                int with_hw_handshake, gn_connection_type device_type,
                struct gn_statemachine *state)
@@ -131,7 +76,7 @@ int device_open(const char *file, int with_odd_parity, int 
with_async,
        /*
         * handle config file connect_script:
         */
-       if (device_script(state->device.fd, "connect_script", state) == -1) {
+       if (device_script(state->device.fd, 1, state) == -1) {
                dprintf("gnokii open device: connect_script failure\n");
                device_close(state);
                return 0;
@@ -147,7 +92,7 @@ void device_close(struct gn_statemachine *state)
        /*
         * handle config file disconnect_script:
         */
-       if (device_script(state->device.fd, "disconnect_script", state) == -1)
+       if (device_script(state->device.fd, 0, state) == -1)
                dprintf("gnokii device close: disconnect_script failure\n");
 
        switch (state->device.type) {
diff --git a/common/forkscript.c b/common/forkscript.c
new file mode 100644
index 00000000..b094ea5f
--- /dev/null
+++ b/common/forkscript.c
@@ -0,0 +1,63 @@
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include "cfgreader.h"
+#include "gnokii-internal.h"
+
+/* Script handling: */
+static void device_script_cfgfunc(const char *section, const char *key, const 
char *value)
+{
+       setenv(key, value, 1); /* errors ignored */
+}
+
+int device_script(int fd, int connect, struct gn_statemachine *state)
+{
+       const char *scriptname, *section;
+       int status;
+       pid_t pid;
+
+       if (connect) {
+               scriptname = state->config.connect_script;
+               section = "connect_script";
+       } else {
+               scriptname = state->config.disconnect_script;
+               section = "disconnect_script";
+       }
+       if (scriptname[0] == '\0')
+               return 0;
+
+       errno = 0;
+       switch ((pid = fork())) {
+       case -1:
+               fprintf(stderr, _("device_script(\"%s\"): fork() failure: 
%s!\n"), scriptname, strerror(errno));
+               return -1;
+
+       case 0: /* child */
+               cfg_foreach(section, device_script_cfgfunc);
+               errno = 0;
+               if (dup2(fd, 0) != 0 || dup2(fd, 1) != 1 || close(fd)) {
+                       fprintf(stderr, _("device_script(\"%s\"): file 
descriptor preparation failure: %s\n"), scriptname, strerror(errno));
+                       _exit(-1);
+               }
+               /* FIXME: close all open descriptors - how to track them?
+                */
+               execl("/bin/sh", "sh", "-c", scriptname, NULL);
+               fprintf(stderr, _("device_script(\"%s\"): script execution 
failure: %s\n"), scriptname, strerror(errno));
+               _exit(-1);
+               /* NOTREACHED */
+
+       default:
+               if (pid == waitpid(pid, &status, 0 /* options */) && 
WIFEXITED(status) && !WEXITSTATUS(status))
+                       return 0;
+               fprintf(stderr, _("device_script(\"%s\"): child script 
execution failure: %s, exit code=%d\n"), scriptname,
+                       (WIFEXITED(status) ? _("normal exit") : _("abnormal 
exit")),
+                       (WIFEXITED(status) ? WEXITSTATUS(status) : -1));
+               errno = EIO;
+               return -1;
+
+       }
+       /* NOTREACHED */
+}
diff --git a/include/gnokii-internal.h b/include/gnokii-internal.h
index c4f6d625..212b0a75 100644
--- a/include/gnokii-internal.h
+++ b/include/gnokii-internal.h
@@ -176,6 +176,8 @@ int utf8_base64_encode(char *dest, int destlen, const char 
*src, int inlen);
 int add_slashes(char *dest, char *src, int maxlen, int len);
 int strip_slashes(char *dest, const char *src, int maxlen, int len);
 
+int device_script(int fd, int connect, struct gn_statemachine *state);
+
 /* authentication for at driver */
 gn_error do_auth(gn_auth_type auth_type, struct gn_statemachine *state);
 
-- 
2.25.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]