add support for specifying process priority
authorFelix Fietkau <nbd@openwrt.org>
Sun, 16 Dec 2012 19:45:42 +0000 (20:45 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 16 Dec 2012 19:45:42 +0000 (20:45 +0100)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
instance.c
instance.h

index b84e7c8ff25b8a3adb78dbe9e746252ca0c6fff7..a25f91a914b252f45e858fd90db26cce9fbf3419 100644 (file)
@@ -1,3 +1,4 @@
+#include <sys/resource.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <net/if.h>
@@ -12,6 +13,7 @@ enum {
        INSTANCE_ATTR_ENV,
        INSTANCE_ATTR_DATA,
        INSTANCE_ATTR_NETDEV,
+       INSTANCE_ATTR_NICE,
        __INSTANCE_ATTR_MAX
 };
 
@@ -20,6 +22,7 @@ static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
        [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
        [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
        [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
+       [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
 };
 
 struct instance_netdev {
@@ -36,6 +39,9 @@ instance_run(struct service_instance *in)
        int argc = 1; /* NULL terminated */
        int rem;
 
+       if (in->nice)
+               setpriority(PRIO_PROCESS, 0, in->nice);
+
        blobmsg_for_each_attr(cur, in->command, rem)
                argc++;
 
@@ -129,6 +135,9 @@ instance_config_changed(struct service_instance *in, struct service_instance *in
        if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
                return true;
 
+       if (in->nice != in_new->nice)
+               return true;
+
        return false;
 }
 
@@ -176,6 +185,12 @@ instance_config_parse(struct service_instance *in)
 
        in->command = cur;
 
+       if ((cur = tb[INSTANCE_ATTR_NICE])) {
+               in->nice = (int8_t) blobmsg_get_u32(cur);
+               if (in->nice < -20 || in->nice > 20)
+                       return false;
+       }
+
        if ((cur = tb[INSTANCE_ATTR_ENV])) {
                if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
                        return false;
index 59e887c0ef850e163b0fbe8dba39198d141718a1..484c856c3e93cc5d16c0a68c850e6fe274c79458 100644 (file)
@@ -10,8 +10,10 @@ struct service_instance {
        struct service *srv;
        const char *name;
 
+       int8_t nice;
        bool valid;
        bool restart;
+
        struct blob_attr *config;
        struct uloop_process proc;
        struct uloop_timeout timeout;