Changeset 33696 in vbox for trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControlExec.cpp
- Timestamp:
- Nov 2, 2010 3:37:54 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControlExec.cpp
r33558 r33696 1028 1028 1029 1029 1030 /** @todo Maybe we want to have an own IPRT function for that! */ 1031 int VBoxServiceControlExecMakeFullPath(const char *pszPath, char *pszExpanded, size_t cbExpanded) 1032 { 1033 int rc = VINF_SUCCESS; 1034 #ifdef RT_OS_WINDOWS 1035 if (!ExpandEnvironmentStrings(pszPath, pszExpanded, cbExpanded)) 1036 rc = RTErrConvertFromWin32(GetLastError()); 1037 #else 1038 /* No expansion for non-Windows yet. */ 1039 rc = RTStrCopy(pszExpanded, cbExpanded, pszPath); 1040 #endif 1041 return rc; 1042 } 1043 1044 1045 int VBoxServiceControlExecResolveExecutable(const char *pszFileName, char *pszResolved, size_t cbResolved) 1046 { 1047 int rc = VINF_SUCCESS; 1048 1049 /* Search the path of our executable. */ 1050 char szVBoxService[RTPATH_MAX]; 1051 if (RTProcGetExecutableName(szVBoxService, sizeof(szVBoxService))) 1052 { 1053 char *pszExecResolved = NULL; 1054 if ( (g_pszProgName && RTStrICmp(pszFileName, g_pszProgName) == 0) 1055 || !RTStrICmp(pszFileName, VBOXSERVICE_NAME)) 1056 { 1057 /* We just want to execute VBoxService (no toolbox). */ 1058 pszExecResolved = RTStrDup(szVBoxService); 1059 } 1030 1060 #ifdef VBOXSERVICE_TOOLBOX 1031 /** 1032 * Constructs the argv command line of a VBoxService toolbox program 1061 else if (RTStrStr(pszFileName, "vbox_") == pszFileName) 1062 { 1063 /* We want to use the internal toolbox (all internal 1064 * tools are starting with "vbox_" (e.g. "vbox_cat"). */ 1065 pszExecResolved = RTStrDup(szVBoxService); 1066 } 1067 #endif 1068 else /* Nothing to resolve, copy original. */ 1069 pszExecResolved = RTStrDup(pszFileName); 1070 AssertPtr(pszExecResolved); 1071 1072 rc = VBoxServiceControlExecMakeFullPath(pszExecResolved, pszResolved, cbResolved); 1073 RTStrFree(pszExecResolved); 1074 } 1075 return rc; 1076 } 1077 1078 1079 #ifdef VBOXSERVICE_TOOLBOX 1080 /** 1081 * Constructs the argv command line of a VBoxService program 1033 1082 * by first appending the full path of VBoxService along with the given 1034 1083 * tool name (e.g. "vbox_cat") + the tool's actual command line parameters. … … 1040 1089 * Needs to be freed with RTGetOptArgvFree. 1041 1090 */ 1042 int VBoxServiceControlExecPrepare ToolboxArgv(const char *pszFileName,1043 1091 int VBoxServiceControlExecPrepareArgv(const char *pszFileName, 1092 const char * const *papszArgs, char ***ppapszArgv) 1044 1093 { 1045 1094 AssertPtrReturn(pszFileName, VERR_INVALID_PARAMETER); … … 1052 1101 if (RT_SUCCESS(rc)) 1053 1102 { 1054 char *pszNewArgs;1055 1103 /* 1056 1104 * Construct the new command line by appending the actual 1057 1105 * tool name to new process' command line. 1058 1106 */ 1059 if (RTStrAPrintf(&pszNewArgs, "%s %s", pszFileName, pszArgs)) 1060 { 1061 int iNumArgsIgnored; 1062 rc = RTGetOptArgvFromString(ppapszArgv, &iNumArgsIgnored, 1063 pszNewArgs, NULL /* Use standard separators. */); 1064 RTStrFree(pszNewArgs); 1107 char szArgsExp[RTPATH_MAX]; 1108 rc = VBoxServiceControlExecMakeFullPath(pszArgs, szArgsExp, sizeof(szArgsExp)); 1109 if (RT_SUCCESS(rc)) 1110 { 1111 char *pszNewArgs; 1112 if (RTStrAPrintf(&pszNewArgs, "%s %s", pszFileName, szArgsExp)) 1113 { 1114 int iNumArgsIgnored; 1115 rc = RTGetOptArgvFromString(ppapszArgv, &iNumArgsIgnored, 1116 pszNewArgs, NULL /* Use standard separators. */); 1117 RTStrFree(pszNewArgs); 1118 } 1065 1119 } 1066 1120 RTStrFree(pszArgs); … … 1118 1172 } 1119 1173 #endif /* RT_OS_WINDOWS */ 1120 #ifdef VBOXSERVICE_TOOLBOX 1121 /* Search the path of our executable. */ 1122 char szVBoxService[RTPATH_MAX]; 1123 if (RTProcGetExecutableName(szVBoxService, sizeof(szVBoxService))) 1124 { 1125 char *pszCmdTool = NULL; 1126 char **papszArgsTool = NULL; 1127 if ( (g_pszProgName && RTStrICmp(pszExec, g_pszProgName) == 0) 1128 || !RTStrICmp(pszExec, VBOXSERVICE_NAME)) 1129 { 1130 /* We just want to execute VBoxService (no toolbox). */ 1131 pszCmdTool = RTStrDup(szVBoxService); 1132 } 1133 else if (RTStrStr(pszExec, "vbox_") == pszExec) 1134 { 1135 /* We want to use the internal toolbox (all internal 1136 * tools are starting with "vbox_" (e.g. "vbox_cat"). */ 1137 pszCmdTool = RTStrDup(szVBoxService); 1138 rc = VBoxServiceControlExecPrepareToolboxArgv(pszCmdTool, papszArgs, &papszArgsTool); 1139 } 1140 1141 if (RT_SUCCESS(rc) && pszCmdTool) 1142 { 1143 /* Disable service flag bit, because we're executing the internal 1144 * tool with the profile which was used to start VBoxService. */ 1145 fFlags &= ~RTPROC_FLAGS_SERVICE; 1146 1147 rc = RTProcCreateEx(pszCmdTool, papszArgsTool ? papszArgsTool : papszArgs, 1148 hEnv, fFlags, 1174 1175 /* 1176 * Do the environment variables expansion on executable and arguments. 1177 */ 1178 char szExecExp[RTPATH_MAX]; 1179 rc = VBoxServiceControlExecResolveExecutable(pszExec, szExecExp, sizeof(szExecExp)); 1180 if (RT_SUCCESS(rc)) 1181 { 1182 char **papszArgsExp; 1183 rc = VBoxServiceControlExecPrepareArgv(szExecExp, papszArgs, &papszArgsExp); 1184 if (RT_SUCCESS(rc)) 1185 { 1186 /* If no user name specified run with current credentials. 1187 * This is prohibited via official Main API! */ 1188 if (!strlen(pszAsUser)) 1189 fFlags &= ~RTPROC_FLAGS_SERVICE; 1190 1191 /* Do normal execution. */ 1192 rc = RTProcCreateEx(szExecExp, papszArgsExp, hEnv, fFlags, 1149 1193 phStdIn, phStdOut, phStdErr, 1150 1194 strlen(pszAsUser) ? pszAsUser : NULL, 1151 1195 strlen(pszPassword) ? pszPassword : NULL, 1152 1196 phProcess); 1153 if (papszArgsTool) 1154 RTGetOptArgvFree(papszArgsTool); 1155 RTStrFree(pszCmdTool); 1156 return rc; 1157 } 1158 } 1159 #endif 1160 1161 /* If no user name specified run with current credentials. 1162 * This is prohibited via official Main API! */ 1163 if (!strlen(pszAsUser)) 1164 fFlags &= ~RTPROC_FLAGS_SERVICE; 1165 1166 /* Do normal execution. */ 1167 rc = RTProcCreateEx(pszExec, papszArgs, hEnv, fFlags, 1168 phStdIn, phStdOut, phStdErr, 1169 strlen(pszAsUser) ? pszAsUser : NULL, 1170 strlen(pszPassword) ? pszPassword : NULL, 1171 phProcess); 1197 } 1198 RTGetOptArgvFree(papszArgsExp); 1199 } 1172 1200 return rc; 1173 1201 }
Note:
See TracChangeset
for help on using the changeset viewer.