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

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

1

2
3
4
5
6
7
8
9
10


11
12
13
14
15
16
17
#define _LINUX_SOURCE 1

#include <sys/syscall.h>
#include <sys/termios.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/ioctl.h>


#include <sys/swap.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

>









>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#define _LINUX_SOURCE 1
#include <sys/resource.h>
#include <sys/syscall.h>
#include <sys/termios.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/klog.h>
#include <sys/swap.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
40
41
42
43
44
45
46


















47
48
49
50
51
52
53
#endif
#ifndef MNT_EXPIRE
#define MNT_EXPIRE 4
#endif
#ifndef MS_MOVE
#define MS_MOVE 8192
#endif



















/* Simple macros */
#ifndef MAX
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
#endif
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#endif
#ifndef MNT_EXPIRE
#define MNT_EXPIRE 4
#endif
#ifndef MS_MOVE
#define MS_MOVE 8192
#endif
#ifndef SYSLOG_ACTION_CLOSE
#define SYSLOG_ACTION_CLOSE 0
#endif
#ifndef SYSLOG_ACTION_OPEN
#define SYSLOG_ACTION_OPEN 1
#endif
#ifndef SYSLOG_ACTION_READ_ALL
#define SYSLOG_ACTION_READ_ALL 3
#endif
#ifndef SYSLOG_ACTION_CLEAR
#define SYSLOG_ACTION_CLEAR 5
#endif
#ifndef SYSLOG_ACTION_CONSOLE_OFF
#define SYSLOG_ACTION_CONSOLE_OFF 6
#endif
#ifndef SYSLOG_ACTION_CONSOLE_ON
#define SYSLOG_ACTION_CONSOLE_ON 7
#endif

/* Simple macros */
#ifndef MAX
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
#endif
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
1966
1967
1968
1969
1970
1971
1972























































































































































































































































1973
1974
1975
1976
1977
1978
1979

	if (retobj != NULL) {
		Tcl_SetObjResult(interp, retobj);
	}

	return(retval);
}
























































































































































































































































#ifndef DISABLE_UNIX_SOCKETS
struct tuapi_socket_unix__chan_id {
	int fd;
	Tcl_Channel chan;
};








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247

	if (retobj != NULL) {
		Tcl_SetObjResult(interp, retobj);
	}

	return(retval);
}

static int tuapi_rlimit(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
	Tcl_Obj *operation_obj, *resource_id_obj, *resource_val_item_obj, *resource_val_itemval_obj, *ret_obj;
	struct rlimit resource_val;
	Tcl_WideInt resource_val_item;
	int resource_id;
	int rlimit_ret, tcl_ret;

	if (objc < 3) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj("wrong # args: should be \"::tuapi::syscall::rlimit operation resource ?value?\"", -1));

		return(TCL_ERROR);
	}

	operation_obj = objv[1];
	resource_id_obj = objv[2];

	switch (tuapi_internal_simplehash_obj(resource_id_obj)) {
		case 0x20d3LU: /* AS */
			resource_id = RLIMIT_AS;
			break;
		case 0x873e945LU: /* CORE */
			resource_id = RLIMIT_CORE;
			break;
		case 0x10e855LU: /* CPU */
			resource_id = RLIMIT_CPU;
			break;
		case 0x8906a41LU: /* DATA */
			resource_id = RLIMIT_DATA;
			break;
		case 0x6a726f45LU: /* FSIZE */
			resource_id = RLIMIT_FSIZE;
			break;
#ifdef RLIMIT_LOCKS
		case 0xc9f0e7d3LU: /* LOCKS */
			resource_id = RLIMIT_LOCKS;
			break;
#endif
		case 0xd908f7cbLU: /* MEMLOCK */
			resource_id = RLIMIT_MEMLOCK;
			break;
#ifdef RLIMIT_MSGQUEUE
		case 0x57167445LU: /* MSGQUEUE */
			resource_id = RLIMIT_MSGQUEUE;
			break;
#endif
		case 0x9d261c5LU: /* NICE */
			resource_id = RLIMIT_NICE;
			break;
		case 0xf8d35c45LU: /* NOFILE */
		case 0xf8d26445LU: /* OFILE */
			resource_id = RLIMIT_NOFILE;
			break;
		case 0xea14a5c3LU: /* NPROC */
			resource_id = RLIMIT_NPROC;
			break;
		case 0x14a9d3LU: /* RSS */
			resource_id = RLIMIT_RSS;
			break;
#ifdef RLIMIT_RTPRIO
		case 0x4a15ee4fLU: /* RTPRIO */
			resource_id = RLIMIT_RTPRIO;
			break;
#endif
#ifdef RLIMIT_RTTIME
		case 0x4a932c45LU: /* RTTIME */
			resource_id = RLIMIT_RTTIME;
			break;
#endif
#ifdef RLIMIT_SIGPENDING
		case 0x2f390347LU: /* SIGPENDING */
			resource_id = RLIMIT_SIGPENDING;
			break;
#endif
		case 0x3a90634bLU: /* STACK */
			resource_id = RLIMIT_STACK;
			break;
		default:
			Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid resource", -1));

			return(TCL_ERROR);
	}

	if (strcmp(Tcl_GetString(operation_obj), "get") == 0) {
		if (objc != 3) {
			Tcl_SetObjResult(interp, Tcl_NewStringObj("wrong # args: should be \"::tuapi::syscall::rlimit get resource\"", -1));

			return(TCL_ERROR);
		}

		rlimit_ret = getrlimit(resource_id, &resource_val);
		if (rlimit_ret != 0) {
			Tcl_SetObjResult(interp, Tcl_NewStringObj("getrlimit() failed", -1));

			return(TCL_ERROR);
		}

		ret_obj = Tcl_NewObj();
		Tcl_ListObjAppendElement(interp, ret_obj, Tcl_NewStringObj("soft", -1));
		Tcl_ListObjAppendElement(interp, ret_obj, Tcl_NewWideIntObj(resource_val.rlim_cur));
		Tcl_ListObjAppendElement(interp, ret_obj, Tcl_NewStringObj("hard", -1));
		Tcl_ListObjAppendElement(interp, ret_obj, Tcl_NewWideIntObj(resource_val.rlim_max));

		Tcl_SetObjResult(interp, ret_obj);

		return(TCL_OK);
	}

	if (strcmp(Tcl_GetString(operation_obj), "set") == 0) {
		if (objc != 4) {
			Tcl_SetObjResult(interp, Tcl_NewStringObj("wrong # args: should be \"::tuapi::syscall::rlimit set resource value\"", -1));

			return(TCL_ERROR);
		}

		resource_val_item_obj = objv[3];

		/* Determine if we were asked to set to a simple value, in which case set both hard and soft limits */
		tcl_ret = Tcl_GetWideIntFromObj(NULL, resource_val_item_obj, &resource_val_item);
		if (tcl_ret == TCL_OK) {
			resource_val.rlim_cur = resource_val_item;
			resource_val.rlim_max = resource_val_item;
		} else {
			tcl_ret = Tcl_DictObjGet(NULL, resource_val_item_obj, Tcl_NewStringObj("soft", -1), &resource_val_itemval_obj);
			if (tcl_ret != TCL_OK) {
				Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid request", -1));

				return(TCL_ERROR);
			}

			tcl_ret = Tcl_GetWideIntFromObj(NULL, resource_val_itemval_obj, &resource_val_item);
			if (tcl_ret != TCL_OK) {
				Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid request", -1));

				return(TCL_ERROR);
			}

			resource_val.rlim_cur = resource_val_item;

			tcl_ret = Tcl_DictObjGet(NULL, resource_val_item_obj, Tcl_NewStringObj("hard", -1), &resource_val_itemval_obj);
			if (tcl_ret != TCL_OK) {
				Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid request", -1));

				return(TCL_ERROR);
			}

			tcl_ret = Tcl_GetWideIntFromObj(NULL, resource_val_itemval_obj, &resource_val_item);
			if (tcl_ret != TCL_OK) {
				Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid request", -1));

				return(TCL_ERROR);
			}

			resource_val.rlim_max = resource_val_item;
		}

		rlimit_ret = setrlimit(resource_id, &resource_val);
		if (rlimit_ret != 0) {
			Tcl_SetObjResult(interp, Tcl_NewStringObj("setrlimit() failed", -1));

			return(TCL_ERROR);
		}

		return(TCL_OK);
	}

	Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid operation", -1));

	return(TCL_ERROR);
}

static int tuapi_klogctl(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
	Tcl_Obj *operation_obj;
	char *buf;
	int buflen;
	int klog_ret;

	if (objc < 2) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj("wrong # args: should be \"::tuapi::syscall::klogctl operation ...\"", -1));

		return(TCL_ERROR);
	}

	klog_ret = klogctl(SYSLOG_ACTION_OPEN, NULL, 0);
	if (klog_ret != 0) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj("klogctl(SYSLOG_ACTION_OPEN, ...) failed", -1));
		return(TCL_ERROR);
	}

	operation_obj = objv[1];
	switch (tuapi_internal_simplehash_obj(operation_obj)) {
		case 0xe5970e4LU: /* read */
			buflen = 256 * 1024;
			buf = malloc(buflen);
			if (buf == NULL) {
				Tcl_SetObjResult(interp, Tcl_NewStringObj("malloc failed !", -1));

				return(TCL_ERROR);
			}

			klog_ret = klogctl(SYSLOG_ACTION_READ_ALL, buf, buflen);
			if (klog_ret == -1) {
				free(buf);

				Tcl_SetObjResult(interp, Tcl_NewStringObj("klogctl(SYSLOG_ACTION_READ_ALL, ...) failed", -1));

				return(TCL_ERROR);
			}

			Tcl_SetObjResult(interp, Tcl_NewByteArrayObj((unsigned char *) buf, klog_ret));

			free(buf);

			return(TCL_OK);
		case 0x3d9973f2LU: /* clear */
			klog_ret = klogctl(SYSLOG_ACTION_CLEAR, NULL, 0);
			if (klog_ret == -1) {
				Tcl_SetObjResult(interp, Tcl_NewStringObj("klogctl(SYSLOG_ACTION_CLEAR, ...) failed", -1));

				return(TCL_ERROR);
			}

			return(TCL_OK);
		case 0x225c336eLU: /* console_on */
			klog_ret = klogctl(SYSLOG_ACTION_CONSOLE_ON, NULL, 0);
			if (klog_ret == -1) {
				Tcl_SetObjResult(interp, Tcl_NewStringObj("klogctl(SYSLOG_ACTION_CONSOLE_ON, ...) failed", -1));

				return(TCL_ERROR);
			}

			return(TCL_OK);
		case 0x2e19bbe6LU: /* console_off */
			klog_ret = klogctl(SYSLOG_ACTION_CONSOLE_OFF, NULL, 0);
			if (klog_ret == -1) {
				Tcl_SetObjResult(interp, Tcl_NewStringObj("klogctl(SYSLOG_ACTION_CONSOLE_OFF, ...) failed", -1));

				return(TCL_ERROR);
			}

			return(TCL_OK);
	}

	Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid subcommand", -1));

	return(TCL_ERROR);
}

#ifndef DISABLE_UNIX_SOCKETS
struct tuapi_socket_unix__chan_id {
	int fd;
	Tcl_Channel chan;
};

2660
2661
2662
2663
2664
2665
2666

2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684

2685
2686
2687
2688
2689
2690
2691

	/* Kernel maintenance related commands */
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::insmod", tuapi_insmod, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::rmmod", tuapi_rmmod, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::lsmod", tuapi_lsmod, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::hostname", tuapi_hostname, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::domainname", tuapi_domainname, NULL, NULL);


	/* Block or char device related commands */
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::losetup", tuapi_losetup, NULL, NULL);

	/* Filesystem related commands */
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::mount", tuapi_mount, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::umount", tuapi_umount, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::swapon", tuapi_swapon, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::swapoff", tuapi_swapoff, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::mknod", tuapi_mknod, NULL, NULL);

	/* Process related commands */
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::getuid", tuapi_getuid, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::chroot", tuapi_chroot, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::pivot_root", tuapi_pivot_root, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::kill", tuapi_kill, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::ps", tuapi_ps, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::execve", tuapi_execve, NULL, NULL);


	/* Network related commands */
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::ifconfig", tuapi_ifconfig, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::route", tuapi_route, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::brctl", tuapi_brctl, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::vconfig", tuapi_vconfig, NULL, NULL);








>


















>







2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961

	/* Kernel maintenance related commands */
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::insmod", tuapi_insmod, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::rmmod", tuapi_rmmod, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::lsmod", tuapi_lsmod, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::hostname", tuapi_hostname, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::domainname", tuapi_domainname, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::klogctl", tuapi_klogctl, NULL, NULL);

	/* Block or char device related commands */
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::losetup", tuapi_losetup, NULL, NULL);

	/* Filesystem related commands */
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::mount", tuapi_mount, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::umount", tuapi_umount, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::swapon", tuapi_swapon, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::swapoff", tuapi_swapoff, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::mknod", tuapi_mknod, NULL, NULL);

	/* Process related commands */
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::getuid", tuapi_getuid, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::chroot", tuapi_chroot, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::pivot_root", tuapi_pivot_root, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::kill", tuapi_kill, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::ps", tuapi_ps, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::execve", tuapi_execve, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::rlimit", tuapi_rlimit, NULL, NULL);

	/* Network related commands */
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::ifconfig", tuapi_ifconfig, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::route", tuapi_route, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::brctl", tuapi_brctl, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::tuapi::syscall::vconfig", tuapi_vconfig, NULL, NULL);