root/rat/trunk/process.c @ 3969

Revision 3969, 4.4 KB (checked in by piers, 7 years ago)

Updates for to bring the code into line with AG sources
- Site app extension
- Per participant volume sliders
- Added a new card defination to auddev_oss

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * FILE:    process.c
3 * PROGRAM: RAT - controller
4 * AUTHOR:  Colin Perkins / Orion Hodson
5 *
6 * Copyright (c) 1999-2001 University College London
7 * All rights reserved.
8 */
9
10#ifndef HIDE_SOURCE_STRINGS
11static const char cvsid[] = "$Id";
12#endif /* HIDE_SOURCE_STRINGS */
13
14#include "config_unix.h"
15#include "config_win32.h"
16#include "debug.h"
17#include "mbus.h"
18#include "process.h"
19
20void fork_process(char *proc_name, char *ctrl_addr, pid_t *pid, int num_tokens, char *token[2])
21{
22#ifdef WIN32
23        char                     args[1024];
24        LPSTARTUPINFO            startup_info;
25        LPPROCESS_INFORMATION    proc_info;
26
27        startup_info = (LPSTARTUPINFO) xmalloc(sizeof(STARTUPINFO));
28        startup_info->cb              = sizeof(STARTUPINFO);
29        startup_info->lpReserved      = 0;
30        startup_info->lpDesktop       = 0;
31        startup_info->lpTitle         = 0;
32        startup_info->dwX             = 0;
33        startup_info->dwY             = 0;
34        startup_info->dwXSize         = 0;
35        startup_info->dwYSize         = 0;
36        startup_info->dwXCountChars   = 0;
37        startup_info->dwYCountChars   = 0;
38        startup_info->dwFillAttribute = 0;
39        startup_info->dwFlags         = 0;
40        startup_info->wShowWindow     = 0;
41        startup_info->cbReserved2     = 0;
42        startup_info->lpReserved2     = 0;
43        startup_info->hStdInput       = 0;
44        startup_info->hStdOutput      = 0;
45        startup_info->hStdError       = 0;
46
47        proc_info = (LPPROCESS_INFORMATION) xmalloc(sizeof(PROCESS_INFORMATION));
48
49        if (num_tokens == 1) {
50                _snprintf(args, sizeof(args), "%s -ctrl \"%s\" -token %s", proc_name, ctrl_addr, token[0]);
51        } else {
52                _snprintf(args, sizeof(args), "%s -T -ctrl \"%s\" -token %s -token %s", proc_name, ctrl_addr, token[0], token[1]);
53        }
54
55        if (!CreateProcess(NULL, args, NULL, NULL, TRUE, 0, NULL, NULL, startup_info, proc_info)) {
56                perror("Couldn't create process");
57                abort();
58        }
59        *pid = (pid_t) proc_info->hProcess;     /* Sigh, hope a HANDLE fits into 32 bits... */
60        debug_msg("Forked %s\n", proc_name);
61#else /* ...we're on unix */
62        char *path, *path_env;
63
64#ifdef DEBUG_FORK
65        if (num_tokens == 1) {
66                debug_msg("%s -ctrl '%s' -token %s\n", proc_name, ctrl_addr, token[0]);
67        } else {
68                debug_msg("%s -T -ctrl '%s' -token %s -token %s\n", proc_name, ctrl_addr, token[0], token[1]);
69        }
70        UNUSED(pid);
71#else
72        if ((getuid() != 0) && (geteuid() != 0)) {
73                /* Ensure that the current directory is in the PATH. This is a security */
74                /* problem, but reduces the number of support calls we get...           */
75                path = getenv("PATH");
76                if (path == NULL) {
77                        path_env = (char *) xmalloc(8);
78                        sprintf(path_env, "PATH=.");
79                } else {
80                        path_env = (char *) xmalloc(strlen(path) + 8);
81                        sprintf(path_env, "PATH=%s:.", path);
82                }
83                debug_msg("%s\n", path_env);
84                putenv(path_env);
85                /* NOTE: we MUST NOT free the memory allocated to path_env. In some    */
86                /* cases the string passed to putenv() becomes part of the environment */
87                /* and hence freeing the memory removes PATH from the environment.     */
88        } else {
89                debug_msg("Running as root? PATH unmodified\n");
90        }
91
92        /* Fork off the sub-process... */
93        *pid = fork();
94        if (*pid == -1) {
95                perror("Cannot fork");
96                abort();
97        } else if (*pid == 0) {
98                if (num_tokens == 1) {
99                        execlp(proc_name, proc_name, "-ctrl", ctrl_addr, "-token", token[0], NULL);
100                } else {
101                        execlp(proc_name, proc_name, "-T", "-ctrl", ctrl_addr, "-token", token[0], "-token", token[1], NULL);
102                }
103                perror("Cannot execute subprocess");
104                /* Note: this MUST NOT be exit() or abort(), since they affect the standard */
105                /* IO channels in the parent process (fork duplicates file descriptors, but */
106                /* they still point to the same underlying file).                           */
107                _exit(1);
108        }
109#endif
110#endif
111}
112
113void kill_process(pid_t proc)
114{
115        if (proc == 0) {
116                debug_msg("Process already dead\n", proc);
117                return;
118        }
119        debug_msg("Killing process %d\n", proc);
120#ifdef WIN32
121        /* This doesn't close down DLLs or free resources, so we have to  */
122        /* hope it doesn't get called. With any luck everything is closed */
123        /* down by sending it an mbus.exit() message, anyway...           */
124        TerminateProcess((HANDLE) proc, 0);
125#else
126        kill(proc, SIGINT);
127#endif
128}
129
Note: See TracBrowser for help on using the browser.