]> arthur.barton.de Git - netatalk.git/commitdiff
RPC basically works, add test suite test for it
authorFrank Lahm <franklahm@googlemail.com>
Wed, 26 Jan 2011 13:27:23 +0000 (14:27 +0100)
committerFrank Lahm <franklahm@googlemail.com>
Wed, 26 Jan 2011 13:27:23 +0000 (14:27 +0100)
configure.in
etc/netalockd/Makefile.am
etc/netalockd/main.c
libatalk/rpc/locking.c
libatalk/util/server_lock.c
test/Makefile.am
test/netalockd/.gitignore [new file with mode: 0644]
test/netalockd/Makefile.am [new file with mode: 0644]
test/netalockd/test.c [new file with mode: 0644]
test/netalockd/test.sh [new file with mode: 0755]

index 0b87ad81375dbeecfaa1f7eaac455596e875d76f..bd9febb160639c157c10b3dae89ab1ee49c489b5 100644 (file)
@@ -1360,6 +1360,7 @@ AC_OUTPUT([Makefile
        sys/ultrix/Makefile
        test/Makefile
        test/afpd/Makefile
+       test/netalockd/Makefile
        ],
        [chmod a+x distrib/config/netatalk-config contrib/shell_utils/apple_*]
 )
index 9d4eb45ae7e7fec69b214d9235008e813022c049..2a42981412bfed16bc815f1927f71a8a4e472ef4 100644 (file)
@@ -4,7 +4,6 @@ sbin_PROGRAMS = netalockd
 
 netalockd_SOURCES = main.c
 netalockd_LDADD = \
-       $(top_builddir)/libevent/libevent.la \
        $(top_builddir)/libatalk/libatalk.la \
        @PTHREAD_LIBS@
 netalockd_LDFLAGS = -static
index 533e14b3c35dc3a6258805e08cd6eafed3571b55..4429c26105002b3dfa7ccd3ce06a8df1be312270 100644 (file)
@@ -149,34 +149,46 @@ static void rpc_teardown(struct evrpc_base *rpcbase)
 int main(int argc, char **argv)
 {
     int ret;
+    int debug = 0;
+    char *address = "127.0.0.1";
+    int port = 4701;
 
     /* Default log setup: log to syslog */
     set_processname("netalockd");
 
-    /* Check lockfile and daemonize */
-    switch(server_lock("netalockd", _PATH_NETALOCKD_LOCK, 0)) {
-    case -1: /* error */
-        exit(EXITERR_SYS);
-    case 0: /* child */
-        break;
-    default: /* server */
-        exit(0);
-    }
-
     char c;
     static char logconfig[MAXPATHLEN + 21 + 1] = "default log_note";
-    char *loglevel, *logfile;
-    while ((c = getopt(argc, argv, "l:f:")) != -1 ) {
+    char *loglevel = NULL, *logfile = NULL;
+    while ((c = getopt(argc, argv, "df:l:n:p:")) != -1 ) {
         switch (c) {
-        case 'l':
-            loglevel = strdup(optarg);
+        case 'd':
+            debug = 1;
             break;
         case 'f':
             logfile = strdup(optarg);
             break;
+        case 'l':
+            loglevel = strdup(optarg);
+            break;
+        case 'n':
+            address = strdup(optarg);
+            break;
+        case 'p':
+            port = atoi(optarg);
+            break;
         }
     }
 
+    /* Check lockfile and daemonize */
+    switch(server_lock("netalockd", _PATH_NETALOCKD_LOCK, debug)) {
+    case -1: /* error */
+        exit(EXITERR_SYS);
+    case 0: /* child */
+        break;
+    default: /* server */
+        exit(0);
+    }
+
     /* Setup logging */
     if (loglevel) {
         strlcpy(logconfig + 8, loglevel, 13);
@@ -193,7 +205,7 @@ int main(int argc, char **argv)
     set_signal();
 
     /* Start listening */
-    if (rpc_setup("127.0.0.1", 4701) != 0) {
+    if (rpc_setup(address, port) != 0) {
         LOG(log_error, logtype_default, "main: rpc setup error");
         exit(1);
     }
index 6916e07d6b2b80e126d8e8eef00b74e1f9497a36..ce43520aff40f9b6f15ee20f2968b4f0bbf4d47b 100644 (file)
@@ -54,17 +54,19 @@ done:
 
 static void rpc_dummy(const char *name)
 {
+    struct lock *lock = NULL;
        struct lock_req *lock_req = NULL;
        struct lock_rep *lock_rep = NULL;
-
+    
+    lock = lock_new();
     lock_req = lock_req_new();
     lock_rep = lock_rep_new();
 
+    EVTAG_ASSIGN(lock_req, req_lock, lock);
     EVTAG_ASSIGN(lock_req, req_filename, name);
 
     EVRPC_MAKE_REQUEST(lock_msg, rpc_pool, lock_req, lock_rep, msg_rep_cb, NULL);
 
-//    event_base_dispatch(ev_base);
     event_dispatch();
 }
 
@@ -94,11 +96,9 @@ int rpc_init(const char *addr, unsigned short port)
     EC_INIT;
     struct evhttp_connection *evcon;
 
-//    EC_NULL_LOG(ev_base = event_base_new());
+    EC_NULL_LOG(ev_base = event_init());
     event_set_log_callback(ev_log_cb);
-//     EC_NULL_LOG(rpc_pool = evrpc_pool_new(ev_base));
        EC_NULL_LOG(rpc_pool = evrpc_pool_new(NULL));
-//     EC_NULL_LOG(evcon = evhttp_connection_new(addr, port));
        EC_NULL_LOG(evcon = evhttp_connection_new(addr, port));
        evrpc_pool_add_connection(rpc_pool, evcon);
 
index a08e30a2f199c5954839bfd82b36282792bdee22..90dc11445eabc56cba7121d7888e15186ab3a30a 100644 (file)
@@ -37,7 +37,8 @@ pid_t server_lock(char *program, char *pidfile, int debug)
   FILE *pf;
   pid_t pid;
   int mask;
-  
+
+  if ( !debug ) {
   mask = umask(022);
   /* check for pid. this can get fooled by stale pid's. */
   if ((pf = fopen(pidfile, "r"))) {
@@ -60,7 +61,7 @@ pid_t server_lock(char *program, char *pidfile, int debug)
   /*
    * Disassociate from controlling tty.
    */
-  if ( !debug ) {
+
     int                i;
 
     getitimer(ITIMER_PROF, &itimer);
@@ -90,10 +91,11 @@ pid_t server_lock(char *program, char *pidfile, int debug)
       fclose(pf);
       return pid;
     }
-  } 
 
   fprintf(pf, "%d\n", getpid());
   fclose(pf);
+  } 
+
   return 0;
 }
 
index bfd9288672bf244ee097d02449e53e88ad04af65..ac00269c30219c81b5c86427a53d990baaf6cae2 100644 (file)
@@ -1 +1 @@
-SUBDIRS = afpd
+SUBDIRS = netalockd afpd
diff --git a/test/netalockd/.gitignore b/test/netalockd/.gitignore
new file mode 100644 (file)
index 0000000..f28e160
--- /dev/null
@@ -0,0 +1,5 @@
+Makefile
+Makefile.in
+.deps
+.libs
+test
diff --git a/test/netalockd/Makefile.am b/test/netalockd/Makefile.am
new file mode 100644 (file)
index 0000000..4d4bb79
--- /dev/null
@@ -0,0 +1,11 @@
+# Makefile.am for test/netalockd/
+
+pkgconfdir = @PKGCONFDIR@
+
+TESTS = test.sh
+EXTRA_DIST = test.sh
+check_PROGRAMS = test
+
+test_SOURCES = test.c
+test_LDADD = $(top_builddir)/libatalk/libatalk.la @PTHREAD_LIBS@
+test_LDFLAGS = -static
diff --git a/test/netalockd/test.c b/test/netalockd/test.c
new file mode 100644 (file)
index 0000000..82ddf34
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2010 Frank Lahm
+ * All Rights Reserved.  See COPYRIGHT.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
+#include <sys/queue.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include "event2/event-config.h"
+
+#include "event2/event.h"
+#include "event2/http.h"
+#include "event2/event_compat.h"
+#include "event2/http_compat.h"
+#include "event2/rpc.h"
+#include "event2/rpc_struct.h"
+
+#include <atalk/logger.h>
+#include <atalk/errchk.h>
+#include <atalk/locking.h>
+#include <atalk/adouble.h>
+#include <atalk/bstrlib.h>
+#include <atalk/bstradd.h>
+
+EVRPC_HEADER(lock_msg, lock_req, lock_rep)
+EVRPC_GENERATE(lock_msg, lock_req, lock_rep)
+
+static struct event_base *ev_base;
+static struct evrpc_pool *rpc_pool;
+
+static void ev_log_cb(int severity, const char *msg)
+{
+    LOG(log_warning, logtype_default, (char *)msg);
+}
+
+static void msg_rep_cb(struct evrpc_status *status,
+                       struct lock_req *req,
+                       struct lock_rep *rep,
+                       void *arg)
+{
+       if (status->error != EVRPC_STATUS_ERR_NONE)
+               goto done;
+
+done:
+    event_base_loopexit(ev_base, NULL);
+}
+
+static void do_one_rpc(const char *name)
+{
+    struct lock *lock = NULL;
+       struct lock_req *lock_req = NULL;
+       struct lock_rep *lock_rep = NULL;
+
+    lock = lock_new();
+    lock_req = lock_req_new();
+    lock_rep = lock_rep_new();
+
+    EVTAG_ASSIGN(lock_req, req_lock, lock);
+    EVTAG_ASSIGN(lock_req, req_filename, name);
+
+    EVRPC_MAKE_REQUEST(lock_msg, rpc_pool, lock_req, lock_rep, msg_rep_cb, NULL);
+
+    event_dispatch();
+}
+
+static int my_rpc_init(const char *addr, unsigned short port)
+{
+    EC_INIT;
+    struct evhttp_connection *evcon;
+
+    EC_NULL_LOG(ev_base = event_init());
+    event_set_log_callback(ev_log_cb);
+       EC_NULL_LOG(rpc_pool = evrpc_pool_new(NULL));
+       EC_NULL_LOG(evcon = evhttp_connection_new(addr, port));
+       evrpc_pool_add_connection(rpc_pool, evcon);
+
+EC_CLEANUP:
+    EC_EXIT;
+}
+
+int main(int argc, char **argv)
+{
+    if (my_rpc_init("127.0.0.1", 4702) != 0)
+        return 1;
+    do_one_rpc("test");
+    return 0;
+}
diff --git a/test/netalockd/test.sh b/test/netalockd/test.sh
new file mode 100755 (executable)
index 0000000..85fbe8b
--- /dev/null
@@ -0,0 +1,7 @@
+#!/bin/sh
+../../etc/netalockd/netalockd -d -p 4702 &
+PID=$!
+sleep 1
+./test
+kill $PID
+exit $?
\ No newline at end of file