private const int StdErrFileNo = 2; // STDERR_FILENO -> standard error file descriptor

        public static void StartBackgroundProcess(ITracer tracer, string programName, string[] args)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo(programName);

            string[] envp = NetCoreMethods.CreateEnvp(startInfo);
            string[] argv = GenerateArgv(startInfo.FileName, args);

            unsafe
            {
                byte **argvPtr = null;
                byte **envpPtr = null;

                try
                {
                    NetCoreMethods.AllocNullTerminatedArray(argv, ref argvPtr);
                    NetCoreMethods.AllocNullTerminatedArray(envp, ref envpPtr);

                    // Fork the child process
                    int processId = Fork();
                    if (processId == -1)
                    {
                        string        errorMessage = "Failed to fork process";
                        EventMetadata metadata     = new EventMetadata();
                        metadata.Add("lastErrorCode", Marshal.GetLastWin32Error());
                        tracer.RelatedError(metadata, errorMessage);
                        throw new Win32Exception(Marshal.GetLastWin32Error(), errorMessage);
                    }

                    if (processId == 0)
                    {
                        RunChildProcess(tracer, programName, argvPtr, envpPtr);
                    }
                }
                finally
                {
                    NetCoreMethods.FreeArray(envpPtr, envp.Length);
                    NetCoreMethods.FreeArray(argvPtr, argv.Length);
                }
            }
        }
Exemple #2
0
        private const int StdErrFileNo = 2; // STDERR_FILENO -> standard error file descriptor

        public static void StartBackgroundProcess(string programName, string[] args)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo(programName);

            string[] envp = NetCoreMethods.CreateEnvp(startInfo);
            string[] argv = GenerateArgv(startInfo.FileName, args);

            unsafe
            {
                byte **argvPtr = null;
                byte **envpPtr = null;

                try
                {
                    NetCoreMethods.AllocNullTerminatedArray(argv, ref argvPtr);
                    NetCoreMethods.AllocNullTerminatedArray(envp, ref envpPtr);

                    // Fork the child process
                    int processId = Fork();
                    if (processId == -1)
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error(), "Failed to fork process");
                    }

                    if (processId == 0)
                    {
                        RunChildProcess(programName, argvPtr, envpPtr);
                    }
                }
                finally
                {
                    NetCoreMethods.FreeArray(envpPtr, envp.Length);
                    NetCoreMethods.FreeArray(argvPtr, argv.Length);
                }
            }
        }