Diff

Differences From Artifact [9210e80361]:

To Artifact [41a8e8e8cb]:


1
2
3
4
5
6
7
8
9
10
11
12

13
14
15
16
17
18
19
#define _LINUX_SOURCE 1
#include <sys/syscall.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 <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <time.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/syscall.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>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
1873
1874
1875
1876
1877
1878
1879
























































































































































































































































































































































































































1880
1881
1882
1883
1884
1885
1886

	if (sock_v6 != -1) {
		close(sock_v6);
	}

	return(retval);
}

























































































































































































































































































































































































































static int tclsystem_tsmf_start_svc(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
	struct timeval select_timeout;
	Tcl_WideInt umask_val, timeout_val, uid_val, gid_val;
	Tcl_Obj *filename_obj, *env_obj, *logfile_obj, **env_entry_objv, *cwd_obj, *umask_obj, *uid_obj, *gid_obj;
	Tcl_Obj *sri_obj, *timeout_obj;
	pid_t child, child_pgid = -1, waitpid_ret;







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







1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
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
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295

	if (sock_v6 != -1) {
		close(sock_v6);
	}

	return(retval);
}

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

static int tclsystem_socket_unix__chan_close(ClientData id_p, Tcl_Interp *interp) {
	struct tclsystem_socket_unix__chan_id *id;
	int fd;

	id = id_p;

	fd = id->fd;

	close(fd);

	free(id);

	return(TCL_OK);
}

static int tclsystem_socket_unix__chan_read(ClientData id_p, char *buf, int bufsize, int *errorCodePtr) {
	struct tclsystem_socket_unix__chan_id *id;
	ssize_t read_ret;
	int fd;
	int retval;

	id = id_p;

	fd = id->fd;

	read_ret = read(fd, buf, bufsize);
	if (read_ret < 0) {
		*errorCodePtr = errno;

		return(-1);
	}

	retval = read_ret;

	return(retval);
}

static int tclsystem_socket_unix__chan_write(ClientData id_p, const char *buf, int toWrite, int *errorCodePtr) {
	struct tclsystem_socket_unix__chan_id *id;
	ssize_t write_ret;
	int fd;
	int bytesWritten;

	id = id_p;

	fd = id->fd;

	bytesWritten = 0;
	while (toWrite) {
		write_ret = write(fd, buf, toWrite);
		if (write_ret == 0) {
			break;
		}

		if (write_ret < 0) {
			*errorCodePtr = errno;

			return(-1);
		}

		toWrite -= write_ret;
		buf += write_ret;
		bytesWritten += write_ret;
	}

	if (bytesWritten == 0) {
		*errorCodePtr = EAGAIN;

		return(-1);
	}

	return(bytesWritten);
}

static void tclsystem_socket_unix__chan_eventhandler(ClientData id_p, int mask) {
	struct tclsystem_socket_unix__chan_id *id;
	Tcl_Channel chan;

	id = id_p;

	chan = id->chan;

	if (!chan) {
		return;
	}

	Tcl_NotifyChannel(chan, mask);
}

static void tclsystem_socket_unix__chan_watch(ClientData id_p, int mask) {
	struct tclsystem_socket_unix__chan_id *id;
	int fd;

	id = id_p;

	fd = id->fd;

	Tcl_CreateFileHandler(fd, mask, tclsystem_socket_unix__chan_eventhandler, id);

	return;
}

static int tclsystem_socket_unix__chan_gethandle(ClientData id_p, int direction, ClientData *handlePtr) {
	struct tclsystem_socket_unix__chan_id *id;
	int fd;
	ClientData fd_cd;

	id = id_p;

	fd = id->fd;

	memcpy(&fd_cd, &fd, sizeof(fd));

	*handlePtr = fd_cd;

	return(TCL_OK);
}

static Tcl_Channel tclsystem_socket_unix_sock2tclchan(int sock) {
	struct tclsystem_socket_unix__chan_id *id;
	static Tcl_ChannelType tcl_chan_type;
	static int tcl_chan_type_init = 0;
	Tcl_Channel tcl_chan;
	char chan_name[32];
	int sock_flags;

	if (!tcl_chan_type_init) {
		tcl_chan_type.typeName = "socket";
		tcl_chan_type.version = TCL_CHANNEL_VERSION_2;
		tcl_chan_type.closeProc = tclsystem_socket_unix__chan_close;
		tcl_chan_type.inputProc = tclsystem_socket_unix__chan_read;
		tcl_chan_type.outputProc = tclsystem_socket_unix__chan_write;
		tcl_chan_type.watchProc = tclsystem_socket_unix__chan_watch;
		tcl_chan_type.getHandleProc = tclsystem_socket_unix__chan_gethandle;
		tcl_chan_type.seekProc = NULL;
		tcl_chan_type.setOptionProc = NULL;
		tcl_chan_type.getOptionProc = NULL;
		tcl_chan_type.close2Proc = NULL;
		tcl_chan_type.blockModeProc = NULL;
		tcl_chan_type.flushProc = NULL;
		tcl_chan_type.handlerProc = NULL;
		tcl_chan_type.wideSeekProc = NULL;
		tcl_chan_type.threadActionProc = NULL;
		tcl_chan_type.truncateProc = NULL;

		tcl_chan_type_init = 1;
	}

	snprintf(chan_name, sizeof(chan_name), "sock%u", sock);

	id = malloc(sizeof(*id));
	if (id == NULL) {
		return(NULL);
	}

	id->fd = sock;
	id->chan = NULL;

	/* Configure socket as non-blocking */
	sock_flags = fcntl(sock, F_GETFL, 0);
	if (sock_flags == -1) {
		sock_flags = O_NONBLOCK;
	} else {
		sock_flags |= O_NONBLOCK;
	}
	fcntl(sock, F_SETFL, (long) sock_flags);

	/* Create the channel */
	tcl_chan = Tcl_CreateChannel(&tcl_chan_type, chan_name, id, TCL_READABLE | TCL_WRITABLE);

	/* Update the structure passed to each function to include the channel name */
	id->chan = tcl_chan;

	return(tcl_chan);
}

struct tclsystem_socket_unix__chan_accept_cd {
	int fd;
	Tcl_Interp *interp;
	Tcl_Obj *command;
};

static void tclsystem_socket_unix__chan_accept(ClientData cd_p, int mask) {
	struct tclsystem_socket_unix__chan_accept_cd *cd;
	Tcl_Interp *interp;
	Tcl_Channel chan;
	Tcl_Obj *command, *command_to_run_objs[5], *command_to_run;
	int setsockopt_ret;
	int pass_creds_true = 1;
	int fd;
	int sock;

	if ((mask & TCL_READABLE) != TCL_READABLE) {
		return;
	}
	
	cd = cd_p;

	fd = cd->fd;
	interp = cd->interp;
	command = cd->command;

	sock = accept(fd, NULL, NULL);
	if (sock < 0) {
		return;
	}

	chan = tclsystem_socket_unix_sock2tclchan(sock);
	if (chan == NULL) {
		close(sock);

		return;
	}

	setsockopt_ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &pass_creds_true, sizeof(pass_creds_true));
	if (setsockopt_ret != 0) {
		close(sock);

		return;
	}

	Tcl_RegisterChannel(interp, chan);

	command_to_run_objs[0] = command;
	command_to_run_objs[1] = Tcl_NewStringObj(Tcl_GetChannelName(chan), -1);
	command_to_run_objs[2] = Tcl_NewStringObj("...uid...", -1); /* XXX: TODO */
	command_to_run_objs[3] = Tcl_NewStringObj("...gid...", -1); /* XXX: TODO */
	command_to_run_objs[4] = Tcl_NewStringObj("...pid...", -1); /* XXX: TODO */
	command_to_run = Tcl_ConcatObj(sizeof(command_to_run_objs) / sizeof(command_to_run_objs[0]), command_to_run_objs);

	Tcl_EvalObjEx(interp, command_to_run, TCL_EVAL_GLOBAL);

	return;
}

static int tclsystem_socket_unix_server(ClientData cd, Tcl_Interp *interp, int sock, const char *path, Tcl_Obj *command) {
	struct tclsystem_socket_unix__chan_accept_cd *accept_cd;
	struct sockaddr_un dest;
	ssize_t pathlen;
	int bind_ret, listen_ret;

	pathlen = strlen(path) + 1;
	if (pathlen <= 0) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj("path too short", -1));

		return(TCL_ERROR);
	}

	if (pathlen > sizeof(dest.sun_path)) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj("path too long", -1));

		return(TCL_ERROR);
	}

	dest.sun_family = AF_UNIX;
	memcpy(dest.sun_path, path, pathlen);

	bind_ret = bind(sock, (struct sockaddr *) &dest, sizeof(dest));
	if (bind_ret != 0) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj(strerror(errno), -1));

		return(TCL_ERROR);
	}

	listen_ret = listen(sock, 2);
	if (listen_ret != 0) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj(strerror(errno), -1));

		return(TCL_ERROR);
	}

	accept_cd = malloc(sizeof(*accept_cd));
	if (accept_cd == NULL) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj(strerror(errno), -1));

		return(TCL_ERROR);
	}

	accept_cd->fd = sock;
	accept_cd->interp = interp;
	accept_cd->command = command;

	Tcl_IncrRefCount(command);

	Tcl_CreateFileHandler(sock, TCL_READABLE, tclsystem_socket_unix__chan_accept, accept_cd);

	return(TCL_OK);
}

static int tclsystem_socket_unix_client(ClientData cd, Tcl_Interp *interp, int sock, const char *path) {
	Tcl_Channel chan;
	struct sockaddr_un dest;
	ssize_t pathlen;
	int connect_ret, setsockopt_ret;
	int pass_creds_true = 1;

	pathlen = strlen(path) + 1;
	if (pathlen <= 0) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj("path too short", -1));

		return(TCL_ERROR);
	}

	if (pathlen > sizeof(dest.sun_path)) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj("path too long", -1));

		return(TCL_ERROR);
	}

	dest.sun_family = AF_UNIX;
	memcpy(dest.sun_path, path, pathlen);

	connect_ret = connect(sock, (struct sockaddr *) &dest, sizeof(dest));
	if (connect_ret != 0) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj(strerror(errno), -1));

		return(TCL_ERROR);
	}

	setsockopt_ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &pass_creds_true, sizeof(pass_creds_true));
	if (setsockopt_ret != 0) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj(strerror(errno), -1));

		return(TCL_ERROR);
	}

	chan = tclsystem_socket_unix_sock2tclchan(sock);
	if (chan == NULL) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj("unable to create Tcl channel", -1));

		return(TCL_ERROR);
	}

	Tcl_RegisterChannel(interp, chan);

	Tcl_SetObjResult(interp, Tcl_NewStringObj(Tcl_GetChannelName(chan), -1));

	return(TCL_OK);
}

static int tclsystem_socket_unix(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
	Tcl_Obj *path_obj, *command_obj;
	char *path;
	int retval;
	int sock;

	if (objc < 2) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj("wrong # args: should be \"::system::syscall::socket_unix path\" or \"::system::syscall::socket_unix -server command path\"", -1));

		return(TCL_ERROR);
	}

	path_obj = objv[1];
	path = Tcl_GetString(path_obj);

	sock = socket(AF_UNIX, SOCK_STREAM, 0);
	if (sock == -1) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj(strerror(errno), -1));

		return(TCL_ERROR);
	}

	if (strcmp(path, "-server") == 0) {
		if (objc != 4) {
			Tcl_SetObjResult(interp, Tcl_NewStringObj("wrong # args: should be \"::system::syscall::socket_unix -server command path\"", -1));

			close(sock);

			return(TCL_ERROR);
		}

		command_obj = objv[2];
		path_obj = objv[3];

		path = Tcl_GetString(path_obj);

		retval = tclsystem_socket_unix_server(cd, interp, sock, path, command_obj);
	} else {
		if (objc != 2) {
			Tcl_SetObjResult(interp, Tcl_NewStringObj("wrong # args: should be \"::system::syscall::socket_unix path\"", -1));

			close(sock);

			return(TCL_ERROR);
		}

		retval = tclsystem_socket_unix_client(cd, interp, sock, path);
	}

	if (retval != TCL_OK) {
		close(sock);
	}

	return(retval);
}
#else
static int tclsystem_socket_unix(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
	Tcl_SetObjResult(interp, Tcl_NewStringObj("not implemented", -1));
	return(TCL_ERROR)
}
#endif

static int tclsystem_tsmf_start_svc(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
	struct timeval select_timeout;
	Tcl_WideInt umask_val, timeout_val, uid_val, gid_val;
	Tcl_Obj *filename_obj, *env_obj, *logfile_obj, **env_entry_objv, *cwd_obj, *umask_obj, *uid_obj, *gid_obj;
	Tcl_Obj *sri_obj, *timeout_obj;
	pid_t child, child_pgid = -1, waitpid_ret;
2180
2181
2182
2183
2184
2185
2186



2187
2188
2189
2190
2191
2192
2193

	/* Network related commands */
	Tcl_CreateObjCommand(interp, "::system::syscall::ifconfig", tclsystem_ifconfig, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::system::syscall::route", tclsystem_route, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::system::syscall::brctl", tclsystem_brctl, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::system::syscall::vconfig", tclsystem_vconfig, NULL, NULL);




	/* Service (TSMF) related commands */
	Tcl_CreateObjCommand(interp, "::system::syscall::tsmf_start_svc", tclsystem_tsmf_start_svc, NULL, NULL);

	/* Internal functions */
	Tcl_CreateObjCommand(interp, "::system::internal::hash", tclsystem_internalproc_simplehash, NULL, NULL);

	/* Define constants */







>
>
>







2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605

	/* Network related commands */
	Tcl_CreateObjCommand(interp, "::system::syscall::ifconfig", tclsystem_ifconfig, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::system::syscall::route", tclsystem_route, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::system::syscall::brctl", tclsystem_brctl, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::system::syscall::vconfig", tclsystem_vconfig, NULL, NULL);

	/* Needed commands for basic services Tcl lacks */
	Tcl_CreateObjCommand(interp, "::system::syscall::socket_unix", tclsystem_socket_unix, NULL, NULL);

	/* Service (TSMF) related commands */
	Tcl_CreateObjCommand(interp, "::system::syscall::tsmf_start_svc", tclsystem_tsmf_start_svc, NULL, NULL);

	/* Internal functions */
	Tcl_CreateObjCommand(interp, "::system::internal::hash", tclsystem_internalproc_simplehash, NULL, NULL);

	/* Define constants */