File: XnLinuxProcesses.cpp

package info (click to toggle)
openni2 2.2.0.33%2Bdfsg-15
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 22,232 kB
  • sloc: cpp: 111,183; ansic: 35,511; sh: 10,542; python: 1,313; java: 952; makefile: 575; xml: 12
file content (151 lines) | stat: -rw-r--r-- 5,339 bytes parent folder | download | duplicates (4)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*****************************************************************************
*                                                                            *
*  PrimeSense PSCommon Library                                               *
*  Copyright (C) 2012 PrimeSense Ltd.                                        *
*                                                                            *
*  This file is part of PSCommon.                                            *
*                                                                            *
*  Licensed under the Apache License, Version 2.0 (the "License");           *
*  you may not use this file except in compliance with the License.          *
*  You may obtain a copy of the License at                                   *
*                                                                            *
*      http://www.apache.org/licenses/LICENSE-2.0                            *
*                                                                            *
*  Unless required by applicable law or agreed to in writing, software       *
*  distributed under the License is distributed on an "AS IS" BASIS,         *
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
*  See the License for the specific language governing permissions and       *
*  limitations under the License.                                            *
*                                                                            *
*****************************************************************************/
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include <XnOS.h>
#include <errno.h>
#if (XN_PLATFORM == XN_PLATFORM_MACOSX || XN_PLATFORM == XN_PLATFORM_ANDROID_ARM)
	#include <sys/wait.h>
#else
	#include <wait.h>
#endif
#include <XnLog.h>

//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------
XN_C_API XnStatus xnOSGetCurrentProcessID(XN_PROCESS_ID* pProcID)
{
	// Validate output pointer
	XN_VALIDATE_OUTPUT_PTR(pProcID);

	// Get the current process id
	*pProcID = getpid();

	// All is good...
	return (XN_STATUS_OK);
}

XN_C_API XnStatus xnOSCreateProcess(const XnChar* strExecutable, XnUInt32 nArgs, const XnChar** pstrArgs, XN_PROCESS_ID* pProcID)
{
	// NOTE: execv, for some weird reason, accepts the arguments as non-const strings, but in the documentation
	// it is explicitly stated that it does not change anything, so we can just use const_cast for it.
	XnChar* astrArgs[100];
	if (nArgs > 98)
	{
		xnLogWarning(XN_MASK_OS, "Too many arguments!");
		return XN_STATUS_OS_PROCESS_CREATION_FAILED;
	}

	astrArgs[0] = const_cast<char*>(strExecutable);
	for (XnUInt i = 0; i < nArgs; ++i)
	{
		astrArgs[i+1] = const_cast<char*>(pstrArgs[i]);
	}
	astrArgs[nArgs+1] = NULL;
	
	pid_t child_pid = fork();
	if (child_pid == -1) // fail
	{
		xnLogWarning(XN_MASK_OS, "Failed to start process! fork() error code is %d.", errno);
		return XN_STATUS_OS_PROCESS_CREATION_FAILED;
	}
	else if (child_pid == 0) // child process
	{
		// fork again
		// we want our process to be totally detached from creator process. To do so, we fork() twice, so that the creator process
		// is terminated, and the new process becomes the child of init.
		pid_t grandchild_pid = fork();
		if (grandchild_pid == -1)
		{
			exit(XN_STATUS_OS_PROCESS_CREATION_FAILED);
		}
		else if (grandchild_pid == 0) // grandchild process
		{
			// close standard streams (this is a background process)
			close(STDIN_FILENO);
			close(STDOUT_FILENO);
			close(STDERR_FILENO);
			
			// detach it from current process (so it will keep running, even if process is terminated)
			setsid();
			
			// load the executable
			if (-1 == execv(strExecutable, astrArgs))
			{
				xnLogWarning(XN_MASK_OS, "Failed to start process! execv() error code is %d.", errno);
				return XN_STATUS_OS_PROCESS_CREATION_FAILED;
			}
		}
		else // child process
		{
			_exit(XN_STATUS_OK);
		}
	}
	else // parent process
	{
		// wait for the child process (the one creating the grandchild process), and make sure it succeeded)
		int status = 0;
		waitpid(child_pid, &status, 0);
		if (status != 0)
		{
			return XN_STATUS_OS_PROCESS_CREATION_FAILED;
		}

		// we have no way to get the grandchild pid
		*pProcID = 0;
	}
	
	return (XN_STATUS_OK);
}

#if XN_PLATFORM == XN_PLATFORM_ANDROID_ARM
static void getApplicationName(XnChar* strAppName, const XnUInt32 nBufferSize)
{
	FILE *pFile;
	size_t length;
	
	pFile = fopen("/proc/self/cmdline", "r");
	length = fread(strAppName, 1, nBufferSize, pFile);
	fclose(pFile);
}

XN_C_API XnStatus XN_C_DECL xnOSGetApplicationFilesDir(XnChar* cpDirName, const XnUInt32 nBufferSize)
{
	char strAppName[1024];

	getApplicationName(strAppName, sizeof(strAppName));
	snprintf(cpDirName, nBufferSize, "/data/data/%s/files/", strAppName);

	return (XN_STATUS_OK);
}

XN_C_API XnStatus XN_C_DECL xnOSGetApplicationLibDir(XnChar* cpDirName, const XnUInt32 nBufferSize)
{
	char strAppName[1024];

	getApplicationName(strAppName, sizeof(strAppName));
	snprintf(cpDirName, nBufferSize, "/data/data/%s/lib/", strAppName);

	return (XN_STATUS_OK);
}
#endif