APIv3: Allow to write application binding 73/17173/2
authorJosé Bollo <jose.bollo@iot.bzh>
Thu, 11 Oct 2018 15:37:02 +0000 (17:37 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Fri, 12 Oct 2018 12:33:44 +0000 (14:33 +0200)
This enable a binding version 3 to declare
no API. This is used to start a job that will
will run after initialisation.

The tutorial tuto-app1 shows how. Run it with:

  afb-daemon --binding tuto-app1.so

A further option could be add to close stdin
even if running in foreground, as it was the
case before.

Change-Id: I2b384d125accb4642eed8e004642ba959326878f
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
bindings/tutorial/CMakeLists.txt
bindings/tutorial/tuto-app1.c [new file with mode: 0644]
src/main-afb-daemon.c

index 2860c8c..5c19bb8 100644 (file)
@@ -32,3 +32,5 @@ ENDMACRO(tuto)
 tuto(1 c)
 tuto(2 c)
 tuto(3 cpp)
+
+tuto(app1 c)
diff --git a/bindings/tutorial/tuto-app1.c b/bindings/tutorial/tuto-app1.c
new file mode 100644 (file)
index 0000000..93747cd
--- /dev/null
@@ -0,0 +1,30 @@
+#include <stdio.h>
+
+#define AFB_BINDING_VERSION 3
+#include <afb/afb-binding.h>
+
+static int appmain(void *arg)
+{
+       const char *name = arg;
+       char buffer[50];
+
+       AFB_API_NOTICE(afbBindingV3root, "Entering Application main");
+       printf("Hello, I'm %s!\n", name);
+       printf("What's your name? ");
+       scanf("%s", buffer);
+       printf("Hi %s! Nice to meet you. OOOOPS I'm late bye bye\n", buffer);
+       return 0;
+}
+
+static void application(int signum, void *arg)
+{
+       if (signum)
+               exit(127);
+       exit(appmain(arg));
+}
+
+int afbBindingV3entry(struct afb_api_x3 *rootapi)
+{
+       return afb_api_queue_job(rootapi, application, "BOB", NULL, 0);
+}
+
index 77d7ab2..65481c4 100644 (file)
@@ -215,13 +215,14 @@ static void setup_daemon()
  +--------------------------------------------------------- */
 static void daemonize()
 {
-       int fd = 0, daemon;
+       int fd = 0, daemon, nostdin;
        const char *output;
        pid_t pid;
 
        daemon = 0;
        output = NULL;
        wrap_json_unpack(main_config, "{s?b s?s}", "daemon", &daemon, "output", &output);
+       nostdin = 0;
 
        if (output) {
                fd = open(output, O_WRONLY | O_APPEND | O_CREAT, 0640);
@@ -241,6 +242,8 @@ static void daemonize()
                }
                if (pid != 0)
                        _exit(0);
+
+               nostdin = 1;
        }
 
        /* closes the input */
@@ -253,8 +256,8 @@ static void daemonize()
                close(fd);
        }
 
-       /* after that ctrl+C still works */
-       close(0);
+       if (nostdin)
+               close(0); /* except if 'daemon', ctrl+C still works after that */
 }
 
 /*---------------------------------------------------------