Check-in [4626ecb025]
Overview
Comment:Added start of TSMF service starter
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 4626ecb025883b8239edcd9b00307370c299f998
User & Date: rkeene on 2012-09-09 07:41:55
Other Links: manifest | tags
Context
2012-09-09
21:18
Started processing "umask" and "cwd" arguments Updated to have 30sec (currently hard-coded) timeout for starting processes check-in: 4b855bb7b2 user: rkeene tags: trunk
07:41
Added start of TSMF service starter check-in: 4626ecb025 user: rkeene tags: trunk
2012-08-22
03:36
Added stub for "vconfig" check-in: 1fce677508 user: rkeene tags: trunk
Changes

Modified system.c from [ea988bbb17] to [af68cfea10].

1
2
3
4
5
6
7
8
9
10

11
12
13
14
15
16

17
18
19
20
21
22
23
#define _LINUX_SOURCE 1
#include <sys/syscall.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/swap.h>
#include <sys/stat.h>

#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>

#include <tcl.h>

#include <linux/sockios.h>
#include <linux/route.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/if_bridge.h>










>






>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#define _LINUX_SOURCE 1
#include <sys/syscall.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.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>
#include <tcl.h>

#include <linux/sockios.h>
#include <linux/route.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/if_bridge.h>
35
36
37
38
39
40
41








42
43
44
45
46
47
48
#endif
#ifndef MNT_EXPIRE
#define MNT_EXPIRE 4
#endif
#ifndef MS_MOVE
#define MS_MOVE 8192
#endif









/* User environment, for execve */
extern char **environ;

/* Re-implement these if needed */
#ifdef SYS_init_module
static int init_module(void *val, unsigned long len, const char *args) {







>
>
>
>
>
>
>
>







37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#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))
#endif

/* User environment, for execve */
extern char **environ;

/* Re-implement these if needed */
#ifdef SYS_init_module
static int init_module(void *val, unsigned long len, const char *args) {
1862
1863
1864
1865
1866
1867
1868













































































































































































































1869
1870
1871
1872
1873
1874
1875

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

	return(retval);
}














































































































































































































int System_Init(Tcl_Interp *interp) {
#ifdef USE_TCL_STUBS
	const char *tclInitStubs_ret;

	/* Initialize Stubs */
	tclInitStubs_ret = Tcl_InitStubs(interp, "8.4", 0);







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







1872
1873
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

	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[]) {
	Tcl_Obj *filename_obj, *env_obj, *logfile_obj, **env_entry_objv;
	pid_t child, child_pgid = -1;
	ssize_t read_ret;
	time_t currtime;
	char *argv[3], *envv[512];
	char *logfile, *filename;
	char logmsg[2048];
	int pipe_ret, setsid_ret, execve_ret, tcl_ret;
	int null_fd, log_fd, tmp_fd, max_fd;
	int env_entry_objc;
	int fds[2], fd;
	int status;
	int idx;

	/* 1. Parse arguments */
	/* 1.a. Ensure the correct number of arguments were passed */
	if (objc != 9) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj("wrong # args: should be \"::system::syscall::tsmf_start_svc sri filename logfile env cwd umask user group\"", -1));

		return(TCL_ERROR);
	}

	/* 1.b. Identify Tcl_Objs to use for each argument */
	filename_obj = objv[2];
	logfile_obj = objv[3];
	env_obj = objv[4];

	/* 1.c. Store string arguments */
	filename = Tcl_GetString(filename_obj);
	logfile = Tcl_GetString(logfile_obj);

	/* 1.d. Process environment */
	tcl_ret = Tcl_ListObjGetElements(interp, env_obj, &env_entry_objc, &env_entry_objv);
	if (tcl_ret != TCL_OK) {
		return(tcl_ret);
	}

	for (idx = 0; idx < MIN(env_entry_objc, sizeof(envv) / sizeof(envv[0]) - 1); idx++) {
		envv[idx] = Tcl_GetString(env_entry_objv[idx]);
	}
	envv[idx] = NULL;

	/* 2. Create a pipe for communication between the processes */
	pipe_ret = pipe(fds);
	if (pipe_ret != 0) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj("pipe failed", -1));

		return(TCL_ERROR);
	}

	/* 3. Fork into a new process */
	child = fork();
	if (child == ((pid_t) -1)) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj("fork failed", -1));

		return(TCL_ERROR);
	}

	if (child != 0) {
		/* 4.parent. Get PGID from child */
		/* 4.parent.a. Close write end of pipe -- we are read-only */
		close(fds[1]);
		fd = fds[0];

		/* 4.parent.b. Read process group ID of child from pipe */
		read_ret = read(fd, &child_pgid, sizeof(child_pgid));

		/* 4.parent.c. Close read end of pipe */
		close(fd);

		/* 4.parent.d. Verify read was meaningful */
		if (read_ret != sizeof(child_pgid)) {
			Tcl_SetObjResult(interp, Tcl_NewStringObj("failed to communicate with started service", -1));

			return(TCL_ERROR);
		}

		/* 4.parent.e. If the PGID given is actually an error, return error */
		if (child_pgid == ((pid_t) -1)) {
			Tcl_SetObjResult(interp, Tcl_NewStringObj("service failed to start", -1));

			return(TCL_ERROR);
		}

		/* 4.parent.f. Return PGID to Tcl */
		Tcl_SetObjResult(interp, Tcl_NewWideIntObj((Tcl_WideInt) child_pgid));

		return(TCL_OK);
	}

	/* 4.child.a. Close read end of pipe -- we only write to it */
	close(fds[0]);
	fd = fds[1];

	/* 5. Create a new session */
	setsid_ret = setsid();
	if (setsid_ret == ((pid_t) -1)) {
		write(fd, &child_pgid, sizeof(child_pgid));

		_exit(0);
	}

	/* 6. Setup environment */
	/* 6.a. Set umask */
	/* XXX: TODO */
	umask(022);

	/* 6.b. Set working directory */
	/* XXX: TODO */
	chdir("/");

	/* 6.c. Open log file for stderr and stdout */
	log_fd = open(logfile, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

	/* 6.d. Open "/dev/null" for stdin */
	null_fd = open("/dev/null", O_RDONLY);
	if (null_fd < 0 || log_fd <0) {
		write(fd, &child_pgid, sizeof(child_pgid));

		_exit(0);
	}

	/* 6.e. Redirect stdin, stdout, and stderr to null, logs */
	dup2(null_fd, STDIN_FILENO);
	dup2(log_fd, STDOUT_FILENO);
	dup2(log_fd, STDERR_FILENO);

	close(null_fd);
	close(log_fd);

	/* 6.f. Close stray file descriptors */
	max_fd = MAX(MAX(MAX(1024, STDIN_FILENO), STDOUT_FILENO), STDERR_FILENO);
	for (tmp_fd = 0; tmp_fd < max_fd; tmp_fd++) {
		if (tmp_fd == STDIN_FILENO || tmp_fd == STDOUT_FILENO || tmp_fd == STDERR_FILENO) {
			continue;
		}

		if (tmp_fd == fd) {
			continue;
		}

		close(tmp_fd);
	}

	/* 6.g. Switch to appropriate user/group */
	/* 6.g.i. Group */
	/* XXX: TODO */

	/* 6.g.ii. User */
	/* XXX: TODO */

	/* 7. Create a new process to actually spawn the process */
	child = fork();
	if (child == ((pid_t) -1)) {
		write(fd, &child_pgid, sizeof(child_pgid));

		_exit(0);
	}

	if (child != 0) {
		/* 7.parent.a. Wait for child process to terminate and collect status */
		waitpid(child, &status, 0);

		/* 7.parent.b. Set PGID (if successful, -1 otherwise) to pass back to TSMF */
		if (status == 0) {
			child_pgid = getpgid(getpid());
		}
		write(fd, &child_pgid, sizeof(child_pgid));

		close(fd);

		/* 7.parent.c. Write log of result */
		/* Note: We avoid ANSI I/O here in case there is already something in the buffer */
		currtime = time(NULL);
		strftime(logmsg, sizeof(logmsg), "[ %b %e %H:%M:%S ", localtime(&currtime));
		write(STDERR_FILENO, logmsg, strlen(logmsg));

		snprintf(logmsg, sizeof(logmsg), "Method \"start\" exited with status %i ]\n", WEXITSTATUS(status));
		write(STDERR_FILENO, logmsg, strlen(logmsg));

		_exit(0);
	}
	
	/* 7.child.a. Close channel to parent */
	close(fd);

	/* 8. Log attempt to run start method */
	currtime = time(NULL);
	strftime(logmsg, sizeof(logmsg), "[ %b %e %H:%M:%S ", localtime(&currtime));
	write(STDERR_FILENO, logmsg, strlen(logmsg));

	snprintf(logmsg, sizeof(logmsg), "Executing start method (\"%s\") ]\n", filename);
	write(STDERR_FILENO, logmsg, strlen(logmsg));

	/* 9. execve() new image */
	argv[0] = filename;
	argv[1] = "start";
	argv[2] = NULL;
	execve_ret = execve(filename, argv, envv);

	/* 10. Abort if something has gone wrong */
	_exit(execve_ret);
}

int System_Init(Tcl_Interp *interp) {
#ifdef USE_TCL_STUBS
	const char *tclInitStubs_ret;

	/* Initialize Stubs */
	tclInitStubs_ret = Tcl_InitStubs(interp, "8.4", 0);
1905
1906
1907
1908
1909
1910
1911



1912
1913
1914
1915
1916
1917
1918

	/* 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);




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

	/* Define constants */
	/** Create parent namespace **/
	Tcl_CreateNamespace(interp, "::system::const", NULL, NULL);








>
>
>







2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136

	/* 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 */
	/** Create parent namespace **/
	Tcl_CreateNamespace(interp, "::system::const", NULL, NULL);