]> git.ipfire.org Git - thirdparty/xtables-addons.git/commitdiff
xt_quota2: allow incremental value to be written to quota proc file
authorSam Liddicott <sam@liddicott.com>
Tue, 7 Jan 2014 17:48:19 +0000 (09:48 -0800)
committerJan Engelhardt <jengelh@inai.de>
Tue, 29 Sep 2015 18:54:18 +0000 (20:54 +0200)
As well as writing absolute numeric values to the quota file, you
can now also write numbers preceded by a + sign or a - sign, e.g.

* "+30" would increase the quota by 30
* "+-20" would increase the quota by negative 20,
  which is the same as decrease by 20
* "-5" would decrease the quota by 5

extensions/xt_quota2.c

index 8a2a509528c951926b0b2c471c17737dab96e1e1..b4d142e39f26575df89765088bb43e3c73b6855b 100644 (file)
@@ -82,7 +82,7 @@ quota_proc_write(struct file *file, const char __user *input,
                  size_t size, loff_t *loff)
 {
        struct xt_quota_counter *e = PDE_DATA(file_inode(file));
-       char buf[sizeof("18446744073709551616")];
+       char buf[sizeof("+-18446744073709551616")];
 
        if (size > sizeof(buf))
                size = sizeof(buf);
@@ -92,9 +92,29 @@ quota_proc_write(struct file *file, const char __user *input,
        if (size < sizeof(buf))
                buf[size] = '\0';
 
-       spin_lock_bh(&e->lock);
-       e->quota = simple_strtoull(buf, NULL, 0);
-       spin_unlock_bh(&e->lock);
+       if (*buf == '+') {
+               int64_t temp = simple_strtoll(buf + 1, NULL, 0);
+               spin_lock_bh(&e->lock);
+               /* Do not let quota become negative if @tmp is very negative */
+               if (temp > 0 || -temp < e->quota)
+                       e->quota += temp;
+               else
+                       e->quota = 0;
+               spin_unlock_bh(&e->lock);
+       } else if (*buf == '-') {
+               int64_t temp = simple_strtoll(buf + 1, NULL, 0);
+               spin_lock_bh(&e->lock);
+               /* Do not let quota become negative if @tmp is very big */
+               if (temp < 0 || temp < e->quota)
+                       e->quota -= temp;
+               else
+                       e->quota = 0;
+               spin_unlock_bh(&e->lock);
+       } else {
+               spin_lock_bh(&e->lock);
+               e->quota = simple_strtoull(buf, NULL, 0);
+               spin_unlock_bh(&e->lock);
+       }
        return size;
 }