Check-in [9b491cd779]
Overview
SHA1:9b491cd779694d2aec49fa71f4f8677bc299a173
Date: 2014-11-03 16:34:22
User: rkeene
Comment:Implemented rlimit and klogctl
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2014-11-03
17:15
[55756e0014] Added UNIX command-workalikes for dmesg/ulimit (user: rkeene, tags: trunk)
16:34
[9b491cd779] Implemented rlimit and klogctl (user: rkeene, tags: trunk)
2014-10-30
19:16
[4ee619220a] Added "ps" command (user: rkeene, tags: trunk)
Changes

Modified tuapi.c from [f152339f63] to [d5c20667b1].

     1      1   #define _LINUX_SOURCE 1
            2  +#include <sys/resource.h>
     2      3   #include <sys/syscall.h>
     3      4   #include <sys/termios.h>
     4      5   #include <netinet/in.h>
     5      6   #include <arpa/inet.h>
     6      7   #include <sys/socket.h>
     7      8   #include <sys/select.h>
     8      9   #include <sys/mount.h>
     9     10   #include <sys/types.h>
    10     11   #include <sys/ioctl.h>
           12  +#include <sys/time.h>
           13  +#include <sys/klog.h>
    11     14   #include <sys/swap.h>
    12     15   #include <sys/stat.h>
    13     16   #include <sys/wait.h>
    14     17   #include <sys/un.h>
    15     18   #include <stdlib.h>
    16     19   #include <signal.h>
    17     20   #include <unistd.h>
................................................................................
    40     43   #endif
    41     44   #ifndef MNT_EXPIRE
    42     45   #define MNT_EXPIRE 4
    43     46   #endif
    44     47   #ifndef MS_MOVE
    45     48   #define MS_MOVE 8192
    46     49   #endif
           50  +#ifndef SYSLOG_ACTION_CLOSE
           51  +#define SYSLOG_ACTION_CLOSE 0
           52  +#endif
           53  +#ifndef SYSLOG_ACTION_OPEN
           54  +#define SYSLOG_ACTION_OPEN 1
           55  +#endif
           56  +#ifndef SYSLOG_ACTION_READ_ALL
           57  +#define SYSLOG_ACTION_READ_ALL 3
           58  +#endif
           59  +#ifndef SYSLOG_ACTION_CLEAR
           60  +#define SYSLOG_ACTION_CLEAR 5
           61  +#endif
           62  +#ifndef SYSLOG_ACTION_CONSOLE_OFF
           63  +#define SYSLOG_ACTION_CONSOLE_OFF 6
           64  +#endif
           65  +#ifndef SYSLOG_ACTION_CONSOLE_ON
           66  +#define SYSLOG_ACTION_CONSOLE_ON 7
           67  +#endif
    47     68   
    48     69   /* Simple macros */
    49     70   #ifndef MAX
    50     71   #define MAX(a, b) (((a) < (b)) ? (b) : (a))
    51     72   #endif
    52     73   #ifndef MIN
    53     74   #define MIN(a, b) (((a) < (b)) ? (a) : (b))
................................................................................
  1966   1987   
  1967   1988   	if (retobj != NULL) {
  1968   1989   		Tcl_SetObjResult(interp, retobj);
  1969   1990   	}
  1970   1991   
  1971   1992   	return(retval);
  1972   1993   }
         1994  +
         1995  +static int tuapi_rlimit(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
         1996  +	Tcl_Obj *operation_obj, *resource_id_obj, *resource_val_item_obj, *resource_val_itemval_obj, *ret_obj;
         1997  +	struct rlimit resource_val;
         1998  +	Tcl_WideInt resource_val_item;
         1999  +	int resource_id;
         2000  +	int rlimit_ret, tcl_ret;
         2001  +
         2002  +	if (objc < 3) {
         2003  +		Tcl_SetObjResult(interp, Tcl_NewStringObj("wrong # args: should be \"::tuapi::syscall::rlimit operation resource ?value?\"", -1));
         2004  +
         2005  +		return(TCL_ERROR);
         2006  +	}
         2007  +
         2008  +	operation_obj = objv[1];
         2009  +	resource_id_obj = objv[2];
         2010  +
         2011  +	switch (tuapi_internal_simplehash_obj(resource_id_obj)) {
         2012  +		case 0x20d3LU: /* AS */
         2013  +			resource_id = RLIMIT_AS;
         2014  +			break;
         2015  +		case 0x873e945LU: /* CORE */
         2016  +			resource_id = RLIMIT_CORE;
         2017  +			break;
         2018  +		case 0x10e855LU: /* CPU */
         2019  +			resource_id = RLIMIT_CPU;
         2020  +			break;
         2021  +		case 0x8906a41LU: /* DATA */
         2022  +			resource_id = RLIMIT_DATA;
         2023  +			break;
         2024  +		case 0x6a726f45LU: /* FSIZE */
         2025  +			resource_id = RLIMIT_FSIZE;
         2026  +			break;
         2027  +#ifdef RLIMIT_LOCKS
         2028  +		case 0xc9f0e7d3LU: /* LOCKS */
         2029  +			resource_id = RLIMIT_LOCKS;
         2030  +			break;
         2031  +#endif
         2032  +		case 0xd908f7cbLU: /* MEMLOCK */
         2033  +			resource_id = RLIMIT_MEMLOCK;
         2034  +			break;
         2035  +#ifdef RLIMIT_MSGQUEUE
         2036  +		case 0x57167445LU: /* MSGQUEUE */
         2037  +			resource_id = RLIMIT_MSGQUEUE;
         2038  +			break;
         2039  +#endif
         2040  +		case 0x9d261c5LU: /* NICE */
         2041  +			resource_id = RLIMIT_NICE;
         2042  +			break;
         2043  +		case 0xf8d35c45LU: /* NOFILE */
         2044  +		case 0xf8d26445LU: /* OFILE */
         2045  +			resource_id = RLIMIT_NOFILE;
         2046  +			break;
         2047  +		case 0xea14a5c3LU: /* NPROC */
         2048  +			resource_id = RLIMIT_NPROC;
         2049  +			break;
         2050  +		case 0x14a9d3LU: /* RSS */
         2051  +			resource_id = RLIMIT_RSS;
         2052  +			break;
         2053  +#ifdef RLIMIT_RTPRIO
         2054  +		case 0x4a15ee4fLU: /* RTPRIO */
         2055  +			resource_id = RLIMIT_RTPRIO;
         2056  +			break;
         2057  +#endif
         2058  +#ifdef RLIMIT_RTTIME
         2059  +		case 0x4a932c45LU: /* RTTIME */
         2060  +			resource_id = RLIMIT_RTTIME;
         2061  +			break;
         2062  +#endif
         2063  +#ifdef RLIMIT_SIGPENDING
         2064  +		case 0x2f390347LU: /* SIGPENDING */
         2065  +			resource_id = RLIMIT_SIGPENDING;
         2066  +			break;
         2067  +#endif
         2068  +		case 0x3a90634bLU: /* STACK */
         2069  +			resource_id = RLIMIT_STACK;
         2070  +			break;
         2071  +		default:
         2072  +			Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid resource", -1));
         2073  +
         2074  +			return(TCL_ERROR);
         2075  +	}
         2076  +
         2077  +	if (strcmp(Tcl_GetString(operation_obj), "get") == 0) {
         2078  +		if (objc != 3) {
         2079  +			Tcl_SetObjResult(interp, Tcl_NewStringObj("wrong # args: should be \"::tuapi::syscall::rlimit get resource\"", -1));
         2080  +
         2081  +			return(TCL_ERROR);
         2082  +		}
         2083  +
         2084  +		rlimit_ret = getrlimit(resource_id, &resource_val);
         2085  +		if (rlimit_ret != 0) {
         2086  +			Tcl_SetObjResult(interp, Tcl_NewStringObj("getrlimit() failed", -1));
         2087  +
         2088  +			return(TCL_ERROR);
         2089  +		}
         2090  +
         2091  +		ret_obj = Tcl_NewObj();
         2092  +		Tcl_ListObjAppendElement(interp, ret_obj, Tcl_NewStringObj("soft", -1));
         2093  +		Tcl_ListObjAppendElement(interp, ret_obj, Tcl_NewWideIntObj(resource_val.rlim_cur));
         2094  +		Tcl_ListObjAppendElement(interp, ret_obj, Tcl_NewStringObj("hard", -1));
         2095  +		Tcl_ListObjAppendElement(interp, ret_obj, Tcl_NewWideIntObj(resource_val.rlim_max));
         2096  +
         2097  +		Tcl_SetObjResult(interp, ret_obj);
         2098  +
         2099  +		return(TCL_OK);
         2100  +	}
         2101  +
         2102  +	if (strcmp(Tcl_GetString(operation_obj), "set") == 0) {
         2103  +		if (objc != 4) {
         2104  +			Tcl_SetObjResult(interp, Tcl_NewStringObj("wrong # args: should be \"::tuapi::syscall::rlimit set resource value\"", -1));
         2105  +
         2106  +			return(TCL_ERROR);
         2107  +		}
         2108  +
         2109  +		resource_val_item_obj = objv[3];
         2110  +
         2111  +		/* Determine if we were asked to set to a simple value, in which case set both hard and soft limits */
         2112  +		tcl_ret = Tcl_GetWideIntFromObj(NULL, resource_val_item_obj, &resource_val_item);
         2113  +		if (tcl_ret == TCL_OK) {
         2114  +			resource_val.rlim_cur = resource_val_item;
         2115  +			resource_val.rlim_max = resource_val_item;
         2116  +		} else {
         2117  +			tcl_ret = Tcl_DictObjGet(NULL, resource_val_item_obj, Tcl_NewStringObj("soft", -1), &resource_val_itemval_obj);
         2118  +			if (tcl_ret != TCL_OK) {
         2119  +				Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid request", -1));
         2120  +
         2121  +				return(TCL_ERROR);
         2122  +			}
         2123  +
         2124  +			tcl_ret = Tcl_GetWideIntFromObj(NULL, resource_val_itemval_obj, &resource_val_item);
         2125  +			if (tcl_ret != TCL_OK) {
         2126  +				Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid request", -1));
         2127  +
         2128  +				return(TCL_ERROR);
         2129  +			}
         2130  +
         2131  +			resource_val.rlim_cur = resource_val_item;
         2132  +
         2133  +			tcl_ret = Tcl_DictObjGet(NULL, resource_val_item_obj, Tcl_NewStringObj("hard", -1), &resource_val_itemval_obj);
         2134  +			if (tcl_ret != TCL_OK) {
         2135  +				Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid request", -1));
         2136  +
         2137  +				return(TCL_ERROR);
         2138  +			}
         2139  +
         2140  +			tcl_ret = Tcl_GetWideIntFromObj(NULL, resource_val_itemval_obj, &resource_val_item);
         2141  +			if (tcl_ret != TCL_OK) {
         2142  +				Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid request", -1));
         2143  +
         2144  +				return(TCL_ERROR);
         2145  +			}
         2146  +
         2147  +			resource_val.rlim_max = resource_val_item;
         2148  +		}
         2149  +
         2150  +		rlimit_ret = setrlimit(resource_id, &resource_val);
         2151  +		if (rlimit_ret != 0) {
         2152  +			Tcl_SetObjResult(interp, Tcl_NewStringObj("setrlimit() failed", -1));
         2153  +
         2154  +			return(TCL_ERROR);
         2155  +		}
         2156  +
         2157  +		return(TCL_OK);
         2158  +	}
         2159  +
         2160  +	Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid operation", -1));
         2161  +
         2162  +	return(TCL_ERROR);
         2163  +}
         2164  +
         2165  +static int tuapi_klogctl(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
         2166  +	Tcl_Obj *operation_obj;
         2167  +	char *buf;
         2168  +	int buflen;
         2169  +	int klog_ret;
         2170  +
         2171  +	if (objc < 2) {
         2172  +		Tcl_SetObjResult(interp, Tcl_NewStringObj("wrong # args: should be \"::tuapi::syscall::klogctl operation ...\"", -1));
         2173  +
         2174  +		return(TCL_ERROR);
         2175  +	}
         2176  +
         2177  +	klog_ret = klogctl(SYSLOG_ACTION_OPEN, NULL, 0);
         2178  +	if (klog_ret != 0) {
         2179  +		Tcl_SetObjResult(interp, Tcl_NewStringObj("klogctl(SYSLOG_ACTION_OPEN, ...) failed", -1));
         2180  +		return(TCL_ERROR);
         2181  +	}
         2182  +
         2183  +	operation_obj = objv[1];
         2184  +	switch (tuapi_internal_simplehash_obj(operation_obj)) {
         2185  +		case 0xe5970e4LU: /* read */
         2186  +			buflen = 256 * 1024;
         2187  +			buf = malloc(buflen);
         2188  +			if (buf == NULL) {
         2189  +				Tcl_SetObjResult(interp, Tcl_NewStringObj("malloc failed !", -1));
         2190  +
         2191  +				return(TCL_ERROR);
         2192  +			}
         2193  +
         2194  +			klog_ret = klogctl(SYSLOG_ACTION_READ_ALL, buf, buflen);
         2195  +			if (klog_ret == -1) {
         2196  +				free(buf);
         2197  +
         2198  +				Tcl_SetObjResult(interp, Tcl_NewStringObj("klogctl(SYSLOG_ACTION_READ_ALL, ...) failed", -1));
         2199  +
         2200  +				return(TCL_ERROR);
         2201  +			}
         2202  +
         2203  +			Tcl_SetObjResult(interp, Tcl_NewByteArrayObj((unsigned char *) buf, klog_ret));
         2204  +
         2205  +			free(buf);
         2206  +
         2207  +			return(TCL_OK);
         2208  +		case 0x3d9973f2LU: /* clear */
         2209  +			klog_ret = klogctl(SYSLOG_ACTION_CLEAR, NULL, 0);
         2210  +			if (klog_ret == -1) {
         2211  +				Tcl_SetObjResult(interp, Tcl_NewStringObj("klogctl(SYSLOG_ACTION_CLEAR, ...) failed", -1));
         2212  +
         2213  +				return(TCL_ERROR);
         2214  +			}
         2215  +
         2216  +			return(TCL_OK);
         2217  +		case 0x225c336eLU: /* console_on */
         2218  +			klog_ret = klogctl(SYSLOG_ACTION_CONSOLE_ON, NULL, 0);
         2219  +			if (klog_ret == -1) {
         2220  +				Tcl_SetObjResult(interp, Tcl_NewStringObj("klogctl(SYSLOG_ACTION_CONSOLE_ON, ...) failed", -1));
         2221  +
         2222  +				return(TCL_ERROR);
         2223  +			}
         2224  +
         2225  +			return(TCL_OK);
         2226  +		case 0x2e19bbe6LU: /* console_off */
         2227  +			klog_ret = klogctl(SYSLOG_ACTION_CONSOLE_OFF, NULL, 0);
         2228  +			if (klog_ret == -1) {
         2229  +				Tcl_SetObjResult(interp, Tcl_NewStringObj("klogctl(SYSLOG_ACTION_CONSOLE_OFF, ...) failed", -1));
         2230  +
         2231  +				return(TCL_ERROR);
         2232  +			}
         2233  +
         2234  +			return(TCL_OK);
         2235  +	}
         2236  +
         2237  +	Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid subcommand", -1));
         2238  +
         2239  +	return(TCL_ERROR);
         2240  +}
  1973   2241   
  1974   2242   #ifndef DISABLE_UNIX_SOCKETS
  1975   2243   struct tuapi_socket_unix__chan_id {
  1976   2244   	int fd;
  1977   2245   	Tcl_Channel chan;
  1978   2246   };
  1979   2247   
................................................................................
  2660   2928   
  2661   2929   	/* Kernel maintenance related commands */
  2662   2930   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::insmod", tuapi_insmod, NULL, NULL);
  2663   2931   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::rmmod", tuapi_rmmod, NULL, NULL);
  2664   2932   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::lsmod", tuapi_lsmod, NULL, NULL);
  2665   2933   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::hostname", tuapi_hostname, NULL, NULL);
  2666   2934   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::domainname", tuapi_domainname, NULL, NULL);
         2935  +	Tcl_CreateObjCommand(interp, "::tuapi::syscall::klogctl", tuapi_klogctl, NULL, NULL);
  2667   2936   
  2668   2937   	/* Block or char device related commands */
  2669   2938   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::losetup", tuapi_losetup, NULL, NULL);
  2670   2939   
  2671   2940   	/* Filesystem related commands */
  2672   2941   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::mount", tuapi_mount, NULL, NULL);
  2673   2942   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::umount", tuapi_umount, NULL, NULL);
................................................................................
  2678   2947   	/* Process related commands */
  2679   2948   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::getuid", tuapi_getuid, NULL, NULL);
  2680   2949   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::chroot", tuapi_chroot, NULL, NULL);
  2681   2950   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::pivot_root", tuapi_pivot_root, NULL, NULL);
  2682   2951   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::kill", tuapi_kill, NULL, NULL);
  2683   2952   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::ps", tuapi_ps, NULL, NULL);
  2684   2953   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::execve", tuapi_execve, NULL, NULL);
         2954  +	Tcl_CreateObjCommand(interp, "::tuapi::syscall::rlimit", tuapi_rlimit, NULL, NULL);
  2685   2955   
  2686   2956   	/* Network related commands */
  2687   2957   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::ifconfig", tuapi_ifconfig, NULL, NULL);
  2688   2958   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::route", tuapi_route, NULL, NULL);
  2689   2959   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::brctl", tuapi_brctl, NULL, NULL);
  2690   2960   	Tcl_CreateObjCommand(interp, "::tuapi::syscall::vconfig", tuapi_vconfig, NULL, NULL);
  2691   2961