Check-in [4b855bb7b2]
Overview
Comment:Started processing "umask" and "cwd" arguments Updated to have 30sec (currently hard-coded) timeout for starting processes
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:4b855bb7b2f754fd75d909218fa02793b377950a
User & Date: rkeene on 2012-09-09 21:18:51
Other Links: manifest | tags
Context
2012-09-09
21:25
Updated to allow (and require) user to specify timeout check-in: 504add9c98 user: rkeene tags: trunk
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
Changes

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

1874
1875
1876
1877
1878
1879
1880


1881
1882
1883
1884

1885
1886
1887

1888
1889
1890
1891
1892
1893
1894
1895
....
1897
1898
1899
1900
1901
1902
1903

1904
1905
1906




1907
1908
1909
1910



1911








1912
1913
1914
1915
1916
1917
1918
1919
....
1939
1940
1941
1942
1943
1944
1945




















1946

1947
1948
1949
1950
1951
1952
1953
....
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
		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 */
................................................................................
	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]);
................................................................................
	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));
................................................................................
		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) {







>
>
|



>

|

>
|







 







>



>
>
>
>




>
>
>

>
>
>
>
>
>
>
>
|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>







 







<
|


<
|







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
....
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
....
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
....
2019
2020
2021
2022
2023
2024
2025

2026
2027
2028

2029
2030
2031
2032
2033
2034
2035
2036
		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_tclval;
	Tcl_Obj *filename_obj, *env_obj, *logfile_obj, **env_entry_objv, *cwd_obj, *umask_obj, *user_obj, *group_obj, *sri_obj;
	pid_t child, child_pgid = -1;
	ssize_t read_ret;
	time_t currtime;
	mode_t umask_val;
	char *argv[3], *envv[512];
	char *logfile, *filename, *cwd, *user, *group;
	char logmsg[2048];
	fd_set read_fdset;
	int pipe_ret, setsid_ret, execve_ret, tcl_ret, select_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 */
................................................................................
	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 */
	sri_obj = objv[1];
	filename_obj = objv[2];
	logfile_obj = objv[3];
	env_obj = objv[4];
	cwd_obj = objv[5];
	umask_obj = objv[6];
	user_obj = objv[7];
	group_obj = objv[8];

	/* 1.c. Store string arguments */
	filename = Tcl_GetString(filename_obj);
	logfile = Tcl_GetString(logfile_obj);
	cwd = Tcl_GetString(cwd_obj);
	user = Tcl_GetString(user_obj);
	group = Tcl_GetString(group_obj);

	/* 1.d. Integer objects */
	tcl_ret = Tcl_GetWideIntFromObj(interp, umask_obj, &umask_tclval);
	if (tcl_ret != TCL_OK) {
		return(tcl_ret);
	}

	umask_val = umask_tclval;

	/* 1.e. 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]);
................................................................................
	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 */
		select_timeout.tv_sec = 30;
		select_timeout.tv_usec = 0;

		FD_ZERO(&read_fdset);
		FD_SET(fd, &read_fdset);

		select_ret = select(fd + 1, &read_fdset, NULL, NULL, &select_timeout);
		if (select_ret == 0) {
			/* On timeout, terminate starting process */
			child_pgid = getpgid(child);
			if (child_pgid != ((pid_t) -1)) {
				kill(-child_pgid, SIGKILL);
			}

			Tcl_SetObjResult(interp, Tcl_NewStringObj("timeout", -1));

			return(TCL_ERROR);
		}

		if (select_ret > 0) {
			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));
................................................................................
		write(fd, &child_pgid, sizeof(child_pgid));

		_exit(0);
	}

	/* 6. Setup environment */
	/* 6.a. Set umask */

	umask(umask_val);

	/* 6.b. Set working directory */

	chdir(cwd);

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