From 1741eeafae974bee3d62d14abd7a57fbc6bf31f1 Mon Sep 17 00:00:00 2001
From: zesstra <zesstra@zesstra.de>
Date: Fri, 17 Apr 2009 01:19:19 +0200
Subject: [PATCH 1/2] Added new efun: configure_driver().

configure_driver() will configure variuous aspects of the driver at run-time.
The first settings available is now the soft and hard memory limits.
configure_driver() always causes a call to privilege_violation() in the
mudlib master.
---
 doc/efun/configure_driver      |   22 ++++++++++++++++
 doc/master/privilege_violation |    1 +
 mudlib/sys/configuration.h     |    4 +++
 src/efuns.c                    |   54 ++++++++++++++++++++++++++++++++++++++++
 src/efuns.h                    |    1 +
 src/func_spec                  |    1 +
 src/string_spec                |    1 +
 7 files changed, 84 insertions(+), 0 deletions(-)
 create mode 100644 doc/efun/configure_driver

diff --git a/doc/efun/configure_driver b/doc/efun/configure_driver
new file mode 100644
index 0000000..9c1bf77
--- /dev/null
+++ b/doc/efun/configure_driver
@@ -0,0 +1,22 @@
+SYNOPSIS
+        #include <sys/configuration.h>
+
+        void configure_driver (int what, mixed data)
+
+DESCRIPTION
+        Sets the option <what> to the value <data>.
+
+        This function always causes the privilege_violation
+        ("configure_driver", this_object(), what, data).
+
+        <what> == DC_MEMORY_LIMIT
+           Set new soft and hard memory limits for the driver.
+           <data> is expected to be an array with two elements, which have to
+           be integers giving the amount of memory in bytes.
+           ({<soft memory limit>, <hard memory limit>})
+
+HISTORY
+        Introduced in LDMud 3.3.719.
+
+SEE ALSO
+        configure_interactive(E)
diff --git a/doc/master/privilege_violation b/doc/master/privilege_violation
index 675da0b..372a21d 100644
--- a/doc/master/privilege_violation
+++ b/doc/master/privilege_violation
@@ -20,6 +20,7 @@ DESCRIPTION
                           informations.
         configure_interactive Set option <arg2> with value <arg3> as
                           default (<arg>==0) or for object <arg>.
+        configure_driver  Set option <arg1> to value(s) <arg2>.
         enable_telnet     Enable/disable telnet (<arg2>) for object <arg>.
         execute_command   Execute command string <arg2> for the object
                           <arg>.
diff --git a/mudlib/sys/configuration.h b/mudlib/sys/configuration.h
index 3e46b91..e703914 100644
--- a/mudlib/sys/configuration.h
+++ b/mudlib/sys/configuration.h
@@ -8,4 +8,8 @@
  */
 #define IC_MAX_WRITE_BUFFER_SIZE 0
 
+/* Possible options for configure_driver().
+ */
+#define DC_MEMORY_LIMIT 0
+
 #endif /* LPC_CONFIGURATION_H_ */
diff --git a/src/efuns.c b/src/efuns.c
index a3f440e..a297ab7 100644
--- a/src/efuns.c
+++ b/src/efuns.c
@@ -139,6 +139,7 @@
 
 #include "../mudlib/sys/debug_info.h"
 #include "../mudlib/sys/driver_hook.h"
+#include "../mudlib/sys/configuration.h"
 #include "../mudlib/sys/objectinfo.h"
 #include "../mudlib/sys/regexp.h"
 #include "../mudlib/sys/strings.h"
@@ -7766,6 +7767,59 @@ f_sgn (svalue_t *sp)
 
 /*-------------------------------------------------------------------------*/
 svalue_t *
+f_configure_driver (svalue_t *sp)
+
+/* EFUN void configure_driver(int what, mixed data)
+ *
+ * This efun configures several aspects of the driver at run-time.
+ * 
+ * <what> is an identifier the setting:
+ *        - DC_MEMORY_LIMIT (0): configures the memory limits
+ * 
+ * <data> is dependent on <what>:
+ *   DC_MEMORY_LIMIT: ({soft-limit, hard-limit}) both <int>, given in Bytes.
+ *
+ */
+
+{
+
+    // Check for privilege_violation.
+    if (!privilege_violation2(STR_CONFIGURE_DRIVER, sp-1, sp, sp))
+    {
+        return pop_n_elems(2, sp);
+    }
+    
+    switch(sp[-1].u.number) {
+        default:
+            errorf("Illegal value %"PRIdPINT" for configure_driver().\n", sp[-1].u.number);
+            return sp; /* NOTREACHED */
+        case DC_MEMORY_LIMIT:
+            if (sp->type != T_POINTER)
+                efun_arg_error(1, T_POINTER, sp->type, sp);
+            if (VEC_SIZE(sp->u.vec) != 2)
+                errorf("Bad arg 1 to configure_driver(): Invalid array size %"PRIdPINT
+                       ", expected 2.\n"
+                       , VEC_SIZE(sp->u.vec));
+            if (sp->u.vec->item[0].type != T_NUMBER)
+                errorf("Bad arg 1 to configure_driver(): Element 0 is '%s', expected 'int'.\n"
+                       , typename(sp->u.vec->item[0].type));
+            if (sp->u.vec->item[1].type != T_NUMBER)
+                errorf("Bad arg 1 to configure_driver(): Element 1 is '%s', expected 'int'.\n"
+                       , typename(sp->u.vec->item[1].type));
+            if (!set_memory_limit(MALLOC_SOFT_LIMIT, sp->u.vec->item[0].u.number))
+                errorf("Could not set the soft memory limit (%"PRIdPINT") in configure_driver()\n",
+                       sp->u.vec->item[0].u.number);
+            if (!set_memory_limit(MALLOC_HARD_LIMIT, sp->u.vec->item[1].u.number))
+                errorf("Could not set the hard memory limit (%"PRIdPINT") in configure_driver()\n",
+                       sp->u.vec->item[1].u.number);
+            break;
+    }
+    
+    // free arguments
+    return pop_n_elems(2, sp);
+} /* f_configure_driver() */
+/*-------------------------------------------------------------------------*/
+svalue_t *
 v_debug_info (svalue_t *sp, int num_arg)
 
 /* EFUN debug_info()
diff --git a/src/efuns.h b/src/efuns.h
index 344d7ee..3f75a23 100644
--- a/src/efuns.h
+++ b/src/efuns.h
@@ -88,6 +88,7 @@ extern svalue_t *tell_room(svalue_t *sp);
 extern svalue_t *f_ctime(svalue_t *);
 extern svalue_t *v_strftime(svalue_t *, int num_arg);
 extern svalue_t *v_debug_info(svalue_t *sp, int num_arg);
+extern svalue_t *f_configure_driver(svalue_t *);
 extern svalue_t *f_rusage(svalue_t *sp);
 extern svalue_t *f_random(svalue_t *);
 extern svalue_t *f_shutdown(svalue_t *sp);
diff --git a/src/func_spec b/src/func_spec
index e48deca..7ec5c37 100644
--- a/src/func_spec
+++ b/src/func_spec
@@ -602,6 +602,7 @@ int     write_file(string, string, int default: F_CONST0);
 
         /* Driver and System functions */
 
+void    configure_driver(int, mixed);
 mixed   debug_info(int, ...);
 void    debug_message(string, int default: F_CONST0);
 string  expand_define(string, null|string default: F_CONST0);
diff --git a/src/string_spec b/src/string_spec
index 7a37976..9dcded6 100644
--- a/src/string_spec
+++ b/src/string_spec
@@ -72,6 +72,7 @@ ATTACH_ERQ_DEMON   "attach_erq_demon"
 BIND_LAMBDA        "bind_lambda"
 CALL_OUT_INFO      "call_out_info"
 CONFIGURE_INTERACTIVE  "configure_interactive"
+CONFIGURE_DRIVER  "configure_driver"
 SEND_ERQ           "erq"
 INPUT_TO           "input_to"
 SEND_UDP           "send_udp"
-- 
1.6.1

