summaryrefslogtreecommitdiff
path: root/open-vm-tools/lib/include/procMgr.h
blob: a33644acaed7879a84d8e3aa4cb7f761ee90be9b (plain)
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
152
153
154
155
156
157
158
/*********************************************************
 * Copyright (C) 2002-2015 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/


/*
 * procMgr.h --
 *
 *    Process management library.
 *
 */


#ifndef __PROCMGR_H__
#   define __PROCMGR_H__

#include "vm_basic_types.h"
#include "auth.h"
#include "dynarray.h"
#if !defined(_WIN32)
#  include <sys/types.h>
#endif
#include <time.h>

/*
 * Keeps track of the platform-specific handle(s) to an asynchronous process.
 */
typedef struct ProcMgr_AsyncProc ProcMgr_AsyncProc;

#if defined(_WIN32)
   typedef DWORD ProcMgr_Pid;
#else /* POSIX */
   typedef pid_t ProcMgr_Pid;
#endif

/*
 * Process information structure.
 * This holds basic information we return per process
 * when listing process information inside the guest.
 */

typedef struct ProcMgrProcInfo {
   ProcMgr_Pid procId;
   char *procCmdName;             // UTF-8
   char *procCmdLine;             // UTF-8
   char *procOwner;               // UTF-8
#if defined(_WIN32)
   Bool procDebugged;
#endif
   time_t procStartTime;
} ProcMgrProcInfo;

DEFINE_DYNARRAY_TYPE(ProcMgrProcInfo);


typedef struct ProcMgr_ProcArgs {
#if defined(_WIN32)
   /*
    * If a caller needs to use a non-default set of arguments for
    * CreateProcess[AsUser] in ProcMgr_Exec[A]sync, this structure should be used. 
    *
    * - If 'userArgs' is NULL, defaults are used:
    *   - bInheritHandles defaults to TRUE
    *   - lpStartupInfo is instantiated and initialized with:
    *     - cb initialized to size of the object
    *     - dwFlags initialized to STARTF_USESHOWWINDOW
    *     - wShowWindow initialized to SW_MINIMIZE.
    *   - defaults for all other parameters are NULL/FALSE
    *
    * - If 'userArgs' is not NULL, the values in the 'userArgs' object are used
    *   according to the following rules:
    *   - If lpStartupInfo is NULL, it is instantiated and initialized with:
    *     - cb initialized to size of the object
    *     - dwFlags initialized to STARTF_USESHOWWINDOW
    *     - wShowWindow initialized to SW_MINIMIZE.
    *     - The caller would need to do some of this initialization if they set
    *       lpStartupInfo.
    *   - If hToken is set:
    *     - if lpStartupInfo->lpDesktop is not NULL, then it is used directly. Otherwise,
    *       lpStartupInfo->lpDesktop is initialized appropriately.
    *
    *     XXX: Make it more convenient for callers(like ToolsDaemonTcloRunProgramImpl) 
    *     to set just wShowWindow without needing to instantiate and initialize a 
    *     STARTUPINFO object. 
    */
   HANDLE hToken;

   LPCWSTR lpApplicationName;
   LPSECURITY_ATTRIBUTES lpProcessAttributes;
   LPSECURITY_ATTRIBUTES lpThreadAttributes;
   BOOL bInheritHandles;
   DWORD dwCreationFlags;
   LPVOID lpEnvironment;
   LPCWSTR lpCurrentDirectory;
   LPSTARTUPINFO lpStartupInfo;
#else
   /*
    * The environment variables to run the program with. If NULL, use the current
    * environment.
    */
   char **envp;

   /*
    * If non-NULL, the directory to be changed to before the process is
    * started.
    */
   char *workingDirectory;
#endif
} ProcMgr_ProcArgs;




typedef void ProcMgr_Callback(Bool status, void *clientData);

#if defined(_WIN32)
typedef HANDLE Selectable;
#else
typedef int Selectable;
#endif

ProcMgrProcInfoArray *ProcMgr_ListProcesses(void);
void ProcMgr_FreeProcList(ProcMgrProcInfoArray *procList);
Bool ProcMgr_KillByPid(ProcMgr_Pid procId);


Bool ProcMgr_ExecSync(char const *cmd,       // UTF-8
                      ProcMgr_ProcArgs *userArgs);
ProcMgr_AsyncProc *ProcMgr_ExecAsync(char const *cmd,     // UTF-8
                                     ProcMgr_ProcArgs *userArgs);
void ProcMgr_Kill(ProcMgr_AsyncProc *asyncProc);
Selectable ProcMgr_GetAsyncProcSelectable(ProcMgr_AsyncProc *asyncProc);
ProcMgr_Pid ProcMgr_GetPid(ProcMgr_AsyncProc *asyncProc);
Bool ProcMgr_IsAsyncProcRunning(ProcMgr_AsyncProc *asyncProc);
int ProcMgr_GetExitCode(ProcMgr_AsyncProc *asyncProc, int *result);
void ProcMgr_Free(ProcMgr_AsyncProc *asyncProc);
#if !defined(_WIN32)
Bool ProcMgr_ImpersonateUserStart(const char *user,      // UTF-8
                                  AuthToken token);
Bool ProcMgr_ImpersonateUserStop(void);
#endif
Bool ProcMgr_GetImpersonatedUserInfo(char **username, char **homeDir);

#endif /* __PROCMGR_H__ */