Revision: 1764
          http://ipcop.svn.sourceforge.net/ipcop/?rev=1764&view=rev
Author:   owes
Date:     2008-08-31 19:18:09 +0000 (Sun, 31 Aug 2008)

Log Message:
-----------
add long long options, some verbose output and cleanup old stuff

Modified Paths:
--------------
    ipcop/trunk/src/misc-progs/restartntpd.c
    ipcop/trunk/src/rc.d/rc.sysinit

Modified: ipcop/trunk/src/misc-progs/restartntpd.c
===================================================================
--- ipcop/trunk/src/misc-progs/restartntpd.c    2008-08-31 19:07:33 UTC (rev 
1763)
+++ ipcop/trunk/src/misc-progs/restartntpd.c    2008-08-31 19:18:09 UTC (rev 
1764)
@@ -1,8 +1,7 @@
 /*
  * Ipcop helper program - restartntpd
  *
- * Starts or stops the ntpd daemon, synchronizes time using ntpdate.
- * We keep ntpdate for syncing, this may change at some time in the future.
+ * Starts or stops the ntpd daemon.
  *
  *
  * This file is part of the IPCop Firewall.
@@ -28,108 +27,102 @@
  */
 
 #include <errno.h>
-#include <stdio.h>
+#include <fcntl.h>
+#include <getopt.h>
 #include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <stdlib.h>
 #include <sys/types.h>
-#include <fcntl.h>
 #include "common.h"
 #include "setuid.h"
 
 
-#define PARA_BOOT      "boot"
-#define PARA_SYNCNOW   "syncnow"
+void usage(char *prg, int exit_code)
+{
+    printf("Usage: %s [OPTION]\n\n", prg);
+    printf("Options:\n");
+    printf("  -b, --boot            after booting\n");
+    printf("  -v, --verbose         be verbose\n");
+    printf("      --help            display this help and exit\n");
+    exit(exit_code);
+}
 
 
 int main(int argc, char *argv[])
 {
-    int fd, pid;
-    char buffer[STRING_SIZE];
     NODEKV *kv = NULL;
-    int enable = 0;
-    int boot = 0;
-    int syncnow = 0;
-    char server_1[STRING_SIZE];
-    char server_2[STRING_SIZE];
+    int flag_boot = 0;
+    int enabled = 0;
 
+    static struct option long_options[] =
+    {
+        { "boot", no_argument, 0, 'b' },
+        { "verbose", no_argument, 0, 'v' },
+        { "help", no_argument, 0, 'h' },
+        { 0, 0, 0, 0}
+    };
+    int c;
+    int option_index = 0;
+
     if (!(initsetuid()))
         exit(1);
 
-    /* verify parameters */
-    if (argc == 1) {
-        /* no parameters, kill ntpd if running and restart if enabled */
+    while ((c = getopt_long(argc, argv, "bv", long_options, &option_index)) != 
-1) {
+        switch (c) {
+        case 'b':              /* booting */
+            flag_boot = 1;
+            break;
+        case 'v':              /* verbose */
+            flag_verbose++;
+            break;
+        case 'h':
+            usage(argv[0], 0);
+        default:
+            fprintf(stderr, "unknown option\n");
+            usage(argv[0], 1);
+        }
     }
-    else if ((argc == 2) && (strcmp(argv[1], PARA_BOOT) == 0)) {
-        boot = 1;
-    }
-    else if ((argc == 2) && (strcmp(argv[1], PARA_SYNCNOW) == 0)) {
-        syncnow = 1;
-    }
-    else {
-        fprintf(stderr, "Bad parameter!\n");
-        return 1;
-    }
 
     /* Get configuration settings */
+    verbose_printf(1, "Reading NTP settings ... \n");
     if (read_kv_from_file(&kv, "/var/ipcop/time/settings") != SUCCESS) {
         fprintf(stderr, "Cannot read time settings\n");
         return 1;
     }
     if (test_kv(kv, "ENABLED_NTP", "on") == SUCCESS) {
-        enable = 1;
+        verbose_printf(2, "NTPd enabled\n");
+        enabled = 1;
     }
-    find_kv_default(kv, "NTP_ADDR_1", server_1);
-    find_kv_default(kv, "NTP_ADDR_2", server_2);
+    else {
+        verbose_printf(2, "NTPd not enabled\n");
+    }
     free_kv(&kv);
 
-    /* Kill ntpd if running */
-    if ((fd = open("/var/run/ntpd.pid", O_RDONLY)) != -1) {
-        if (read(fd, buffer, STRING_SIZE_LARGE - 1) == -1)
-            fprintf(stderr, "Couldn't read from pid file\n");
-        else {
-            pid = atoi(buffer);
-            if (pid <= 1)
-                fprintf(stderr, "Bad pid value\n");
-            else {
-                if (kill(pid, SIGTERM) == -1)
-                    fprintf(stderr, "Unable to send SIGTERM\n");
-                else {
-                    unlink("/var/run/ntpd.pid");
-                    if (enable) {
-                        // small delay between kill and start
-                        sleep(1);
-                    }
-                }
-            }
+    /* Stop ntpd if running */
+    if (access("/var/run/ntpd.pid", 0) != -1) {
+        verbose_printf(2, "Stopping NTPd ... \n");
+        if (mysignalpidfile("/var/run/ntpd.pid", SIGTERM) != SUCCESS ) {
+            exit(0);
         }
-        close(fd);
+        /* PID exists after stopping */
+        unlink("/var/run/ntpd.pid");
+
+        if (enabled) {
+            /* small delay between stop and start */
+            sleep(1);
+        }
     }
     else {
-        if (errno != ENOENT) {
-            perror("Unable to open pid file");
-            exit(1);
+        if (!flag_boot) {
+            verbose_printf(1, "NTPd not running, not stopping\n");
         }
     }
 
-    /* Forced sync */
-    /* do not test for red active, NTP server could be anywhere, let ppp 
dialup block connection */
-#if USE_FORCE_SYNC
-    if (syncnow || (boot && enable)) {
-        snprintf(buffer, STRING_SIZE, "/usr/bin/ntpdate -U ntp -su %s %s", 
server_1, server_2);
-        if (!safe_system(buffer)) {
-            safe_system("/usr/bin/logger -t ipcop \"ntpdate synchronisation 
event\"");
-            safe_system("/sbin/hwclock --systohc");
-        }
-        else {
-            safe_system("/usr/bin/logger -t ipcop \"ntpdate error\"");
-        }
-    }
-#endif
-
     /* Start ntpd if enabled */
-    if (enable) {
+    if (enabled) {
+        verbose_printf(1, "Starting NTPd ... \n");
         safe_system("/usr/bin/ntpd -p /var/run/ntpd.pid -u ntp:ntp");
     }
     return 0;

Modified: ipcop/trunk/src/rc.d/rc.sysinit
===================================================================
--- ipcop/trunk/src/rc.d/rc.sysinit     2008-08-31 19:07:33 UTC (rev 1763)
+++ ipcop/trunk/src/rc.d/rc.sysinit     2008-08-31 19:18:09 UTC (rev 1764)
@@ -295,7 +295,7 @@
 /bin/dmesg > /var/log/dmesg
 
 echo "Starting ntpd (if enabled)"
-/usr/local/bin/restartntpd boot
+/usr/local/bin/restartntpd --boot
 
 if [ -e /var/log/rrd/disk.rrd ]; then
        echo "Adjusting graphs to compensate for boot"


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Ipcop-svn mailing list
Ipcop-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ipcop-svn

Reply via email to