]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - src/misc-progs/qosctrl.c
Update libvirt to 2.1
[ipfire-2.x.git] / src / misc-progs / qosctrl.c
index ac9fd37504e7ec2c778b80c8e666552ce75111e7..c00dbcf7ceac5f82c52ee71f1fd0f7dc3fc748ed 100644 (file)
 #include <sys/types.h>
 #include <fcntl.h>
 #include "setuid.h"
+#include "libsmooth.h"
+
+#define QOS_SH "/var/ipfire/qos/bin/qos.sh"
 
 int main(int argc, char *argv[]) {
+       struct keyvalue* kv = NULL;
+        int fd = -1;
+       int r = 0;
+
+        if (!(initsetuid()))
+                exit(1);
 
-       int fd = -1;
-       int enable = 0;
-
-       if (!(initsetuid()))
-               exit(1);
-
-       if (argc < 2) {
-               fprintf(stderr, "\nNo argument given.\n\nqosctrl (start|stop|restart|status)\n\n");
-               exit(1);
-       }
-
-
-               if (strcmp(argv[1], "start") == 0) {
-                        if ((fd = open("/var/ipfire/qos/enable", O_RDONLY)) != -1)
-                       {
-                               close(fd);
-                               enable = 1;
-                       }
-
-                       if (enable)
-                       {
-                               safe_system("/var/ipfire/qos/bin/qos.sh start");
-                       }       
-               } else if (strcmp(argv[1], "stop") == 0) {
-                       safe_system("/var/ipfire/qos/bin/qos.sh clear");
-               } else if (strcmp(argv[1], "status") == 0) {
-                       safe_system("/var/ipfire/qos/bin/qos.sh status");
-               } else if (strcmp(argv[1], "restart") == 0) {
-                       safe_system("/var/ipfire/qos/bin/qos.sh restart");
-               } else {
-                       fprintf(stderr, "\nBad argument given.\n\nqosctrl (start|stop|restart|status)\n\n");
-                       exit(1);
+        if (argc < 2) {
+                fprintf(stderr, "\nNo argument given.\n\nqosctrl (start|stop|restart|status|generate)\n\n");
+                exit(1);
+        }
+
+        if (strcmp(argv[1], "generate") == 0) {
+               kv = initkeyvalues();
+               if (!readkeyvalues(kv, CONFIG_ROOT "/qos/settings")) {
+                       fprintf(stderr, "Cannot read QoS settings\n");
+                       r = 1;
+                       goto END;
                }
 
-       return 0;
+               char enabled[STRING_SIZE];
+               if (!findkey(kv, "ENABLED", enabled))
+                       strcpy(enabled, "off");
+
+               if (strcmp(enabled, "on") == 0)
+                       safe_system("/usr/bin/perl /var/ipfire/qos/bin/makeqosscripts.pl > " QOS_SH);
+               else
+                       unlink(QOS_SH);
+        }
+
+        if ((fd = open(QOS_SH, O_RDONLY)) != -1) {
+                close(fd);
+        } else {
+                // If there is no qos.sh do nothing.
+                goto END;
+        }
+
+        safe_system("chmod 755 " QOS_SH " &>/dev/null");
+        if (strcmp(argv[1], "start") == 0) {
+                safe_system(QOS_SH " start");
+        } else if (strcmp(argv[1], "stop") == 0) {
+                safe_system(QOS_SH " clear");
+        } else if (strcmp(argv[1], "status") == 0) {
+                safe_system(QOS_SH " status");
+        } else if (strcmp(argv[1], "restart") == 0) {
+                safe_system(QOS_SH " restart");
+        } else {
+                if (strcmp(argv[1], "generate") == 0) {exit(0);}
+                fprintf(stderr, "\nBad argument given.\n\nqosctrl (start|stop|restart|status|generate)\n\n");
+                exit(1);
+        }
+
+END:
+       if (kv)
+               freekeyvalues(kv);
+
+        return r;
 }