Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

451 lines
10 KiB

  1. /******************************************************************************
  2. Copyright(c) Microsoft Corporation
  3. Module Name:
  4. run.cpp
  5. Abstract:
  6. This module runs the schedule task present in the system
  7. Author:
  8. Venu Gopal Choudary 12-Mar-2001
  9. Revision History:
  10. Venu Gopal Choudary 12-Mar-2001 : Created it
  11. ******************************************************************************/
  12. //common header files needed for this file
  13. #include "pch.h"
  14. #include "CommonHeaderFiles.h"
  15. // Function declaration for the Usage function.
  16. VOID DisplayRunUsage();
  17. /*****************************************************************************
  18. Routine Description:
  19. This routine runs the scheduled task(s)
  20. Arguments:
  21. [ in ] argc : Number of command line arguments
  22. [ in ] argv : Array containing command line arguments
  23. Return Value :
  24. A DWORD value indicating EXIT_SUCCESS on success else
  25. EXIT_FAILURE on failure
  26. *****************************************************************************/
  27. DWORD
  28. RunScheduledTask( DWORD argc, LPCTSTR argv[] )
  29. {
  30. // Variables used to find whether Run option, Usage option
  31. // are specified or not
  32. BOOL bRun = FALSE;
  33. BOOL bUsage = FALSE;
  34. // Set the TaskSchduler object as NULL
  35. ITaskScheduler *pITaskScheduler = NULL;
  36. // Return value
  37. HRESULT hr = S_OK;
  38. // Initialising the variables that are passed to TCMDPARSER structure
  39. _TCHAR szServer[ MAX_STRING_LENGTH ] = NULL_STRING;
  40. _TCHAR szTaskName[ MAX_STRING_LENGTH ] = NULL_STRING;
  41. _TCHAR szUser[ MAX_STRING_LENGTH ] = NULL_STRING;
  42. _TCHAR szPassword[ MAX_STRING_LENGTH ] = NULL_STRING;
  43. // Declarations related to Task name
  44. WCHAR wszJobName[MAX_TASKNAME_LEN] = NULL_U_STRING;
  45. // Dynamic Array contaning array of jobs
  46. TARRAY arrJobs = NULL;
  47. // Loop Variable.
  48. DWORD dwJobCount = 0;
  49. //buffer for displaying error message
  50. TCHAR szMessage[MAX_STRING_LENGTH] = NULL_STRING;
  51. BOOL bNeedPassword = FALSE;
  52. BOOL bResult = FALSE;
  53. BOOL bCloseConnection = TRUE;
  54. lstrcpy( szPassword, ASTERIX );
  55. // Builiding the TCMDPARSER structure
  56. TCMDPARSER cmdOptions[] =
  57. {
  58. {
  59. CMDOPTION_RUN,
  60. CP_MAIN_OPTION,
  61. 1,
  62. 0,
  63. &bRun,
  64. NULL_STRING,
  65. NULL,
  66. NULL
  67. },
  68. {
  69. SWITCH_SERVER,
  70. CP_TYPE_TEXT | CP_VALUE_MANDATORY,
  71. 1,
  72. 0,
  73. &szServer,
  74. NULL_STRING,
  75. NULL,
  76. NULL
  77. },
  78. {
  79. SWITCH_TASKNAME,
  80. CP_TYPE_TEXT | CP_VALUE_MANDATORY | CP_MANDATORY ,
  81. 1,
  82. 0,
  83. &szTaskName,
  84. NULL_STRING,
  85. NULL,
  86. NULL
  87. },
  88. {
  89. CMDOPTION_USAGE,
  90. CP_USAGE ,
  91. 1,
  92. 0,
  93. &bUsage,
  94. NULL_STRING,
  95. NULL,
  96. NULL
  97. },
  98. {
  99. SWITCH_USER,
  100. CP_TYPE_TEXT | CP_VALUE_MANDATORY,
  101. OPTION_COUNT,
  102. 0,
  103. &szUser,
  104. NULL_STRING,
  105. NULL,
  106. NULL
  107. },
  108. {
  109. SWITCH_PASSWORD,
  110. CP_TYPE_TEXT | CP_VALUE_OPTIONAL,
  111. OPTION_COUNT,
  112. 0,
  113. &szPassword,
  114. NULL_STRING,
  115. NULL,
  116. NULL
  117. }
  118. };
  119. // Parsing the run option switches
  120. if ( DoParseParam( argc, argv, SIZE_OF_ARRAY(cmdOptions), cmdOptions ) == FALSE)
  121. {
  122. DISPLAY_MESSAGE( stderr, GetResString(IDS_LOGTYPE_ERROR ));
  123. DISPLAY_MESSAGE( stderr, GetReason() );
  124. //DISPLAY_MESSAGE(stderr, GetResString(IDS_RUN_SYNERROR));
  125. return EXIT_FAILURE;
  126. }
  127. // triming the null spaces
  128. StrTrim(szServer, TRIM_SPACES );
  129. StrTrim(szTaskName, TRIM_SPACES );
  130. // check whether password (-p) specified in the command line or not.
  131. if ( cmdOptions[OI_RUNPASSWORD].dwActuals == 0 )
  132. {
  133. lstrcpy( szPassword, NULL_STRING );
  134. }
  135. // Displaying run usage if user specified -? with -run option
  136. if( bUsage == TRUE )
  137. {
  138. DisplayRunUsage();
  139. return EXIT_SUCCESS;
  140. }
  141. // check for invalid server
  142. if( ( cmdOptions[OI_RUNSERVER].dwActuals == 1 ) && ( lstrlen( szServer ) == 0 ) )
  143. {
  144. DISPLAY_MESSAGE(stderr, GetResString(IDS_NO_SERVER));
  145. return RETVAL_FAIL;
  146. }
  147. // check for invalid user name
  148. if( ( cmdOptions[OI_RUNSERVER].dwActuals == 0 ) && ( cmdOptions[OI_RUNUSERNAME].dwActuals == 1 ) )
  149. {
  150. DISPLAY_MESSAGE(stderr, GetResString(IDS_RUN_USER_BUT_NOMACHINE));
  151. return RETVAL_FAIL;
  152. }
  153. // check for the length of user name
  154. if( ( cmdOptions[OI_RUNSERVER].dwActuals == 1 ) && ( cmdOptions[OI_RUNUSERNAME].dwActuals == 1 ) &&
  155. ( lstrlen( szUser ) == 0 ) )
  156. {
  157. DISPLAY_MESSAGE(stderr, GetResString(IDS_INVALID_USERNAME));
  158. return RETVAL_FAIL;
  159. }
  160. // check whether -ru is specified or not
  161. if ( cmdOptions[ OI_RUNUSERNAME ].dwActuals == 0 &&
  162. cmdOptions[ OI_RUNPASSWORD ].dwActuals == 1 )
  163. {
  164. // invalid syntax
  165. DISPLAY_MESSAGE(stderr, GetResString(IDS_RPASSWORD_BUT_NOUSERNAME));
  166. return RETVAL_FAIL;
  167. }
  168. // check for the length of taskname
  169. if( ( lstrlen( szTaskName ) > MAX_JOB_LEN ) || ( lstrlen(szTaskName) == 0 ) )
  170. {
  171. DISPLAY_MESSAGE(stderr, GetResString( IDS_INVALID_TASKLENGTH ));
  172. return RETVAL_FAIL;
  173. }
  174. // check for the length of username
  175. if ( lstrlen( szUser ) > MAX_USERNAME_LENGTH )
  176. {
  177. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_UNAME));
  178. return RETVAL_FAIL;
  179. }
  180. // check for the length of password
  181. if ( lstrlen( szPassword ) > MAX_PASSWORD_LENGTH )
  182. {
  183. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_PASSWORD));
  184. return RETVAL_FAIL;
  185. }
  186. // Convert the task name specified by the user to wide char or unicode format
  187. if ( GetAsUnicodeString(szTaskName,wszJobName,SIZE_OF_ARRAY(wszJobName)) == NULL )
  188. {
  189. return RETVAL_FAIL;
  190. }
  191. //for holding values of parameters in FormatMessage()
  192. _TCHAR* szValues[1] = {NULL};
  193. // check whether the password (-p) specified in the command line or not
  194. // and also check whether '*' or empty is given for -p or not
  195. if( ( IsLocalSystem( szServer ) == FALSE ) &&
  196. ( ( cmdOptions[OI_RUNPASSWORD].dwActuals == 0 ) || ( lstrcmpi ( szPassword, ASTERIX ) == 0 ) ) )
  197. {
  198. bNeedPassword = TRUE;
  199. }
  200. if( ( IsLocalSystem( szServer ) == FALSE ) || ( cmdOptions[OI_RUNUSERNAME].dwActuals == 1 ) )
  201. {
  202. // Establish the connection on a remote machine
  203. bResult = EstablishConnection(szServer,szUser,SIZE_OF_ARRAY(szUser),szPassword,SIZE_OF_ARRAY(szPassword), bNeedPassword );
  204. if (bResult == FALSE)
  205. {
  206. DISPLAY_MESSAGE( stderr, GetResString(IDS_ERROR_STRING) );
  207. DISPLAY_MESSAGE( stderr, GetReason());
  208. return EXIT_FAILURE ;
  209. }
  210. else
  211. {
  212. // though the connection is successfull, some conflict might have occured
  213. switch( GetLastError() )
  214. {
  215. case I_NO_CLOSE_CONNECTION:
  216. bCloseConnection = FALSE;
  217. break;
  218. case E_LOCAL_CREDENTIALS:
  219. case ERROR_SESSION_CREDENTIAL_CONFLICT:
  220. {
  221. bCloseConnection = FALSE;
  222. DISPLAY_MESSAGE( stderr, GetResString(IDS_ERROR_STRING) );
  223. DISPLAY_MESSAGE( stderr, GetReason());
  224. return EXIT_FAILURE;
  225. }
  226. }
  227. }
  228. }
  229. // Get the task Scheduler object for the machine.
  230. pITaskScheduler = GetTaskScheduler( szServer );
  231. // If the Task Scheduler is not defined then give the error message.
  232. if ( pITaskScheduler == NULL )
  233. {
  234. // close the connection that was established by the utility
  235. if ( bCloseConnection == TRUE )
  236. CloseConnection( szServer );
  237. Cleanup(pITaskScheduler);
  238. return EXIT_FAILURE;
  239. }
  240. // Validate the Given Task and get as TARRAY in case of taskname
  241. arrJobs = ValidateAndGetTasks( pITaskScheduler, szTaskName);
  242. if( arrJobs == NULL )
  243. {
  244. _stprintf( szMessage , GetResString(IDS_TASKNAME_NOTEXIST), szTaskName);
  245. DISPLAY_MESSAGE(stderr, szMessage);
  246. // close the connection that was established by the utility
  247. if ( bCloseConnection == TRUE )
  248. CloseConnection( szServer );
  249. Cleanup(pITaskScheduler);
  250. return EXIT_FAILURE;
  251. }
  252. IPersistFile *pIPF = NULL;
  253. ITask *pITask = NULL;
  254. // returns an pITask inteface for wszJobName
  255. hr = pITaskScheduler->Activate(wszJobName,IID_ITask,
  256. (IUnknown**) &pITask);
  257. if (FAILED(hr))
  258. {
  259. DisplayErrorMsg(hr);
  260. if( pIPF )
  261. pIPF->Release();
  262. if( pITask )
  263. pITask->Release();
  264. // close the connection that was established by the utility
  265. if ( bCloseConnection == TRUE )
  266. CloseConnection( szServer );
  267. Cleanup(pITaskScheduler);
  268. return EXIT_FAILURE;
  269. }
  270. DWORD dwResult = 0;
  271. ULONG ulLong = MAX_RES_STRING;
  272. TCHAR szBuffer[MAX_STRING_LENGTH] = NULL_STRING;
  273. HRESULT phrStatus = S_OK;
  274. _TCHAR szTaskProperty[MAX_STRING_LENGTH] = NULL_STRING;
  275. // get the status code
  276. hr = GetStatusCode(pITask,szTaskProperty);
  277. if (FAILED(hr))
  278. {
  279. DisplayErrorMsg(hr);
  280. if( pIPF )
  281. pIPF->Release();
  282. if( pITask )
  283. pITask->Release();
  284. // close the connection that was established by the utility
  285. if ( bCloseConnection == TRUE )
  286. CloseConnection( szServer );
  287. Cleanup(pITaskScheduler);
  288. return EXIT_FAILURE;
  289. }
  290. // check whether the task already been running or not
  291. if ( (lstrcmpi(szTaskProperty , GetResString(IDS_STATUS_RUNNING)) == 0 ))
  292. {
  293. szValues[0] = (_TCHAR*) (wszJobName);
  294. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  295. GetResString(IDS_RUNNING_ALREADY),0,MAKELANGID(LANG_NEUTRAL,
  296. SUBLANG_DEFAULT),szBuffer,
  297. MAX_STRING_LENGTH,(va_list*)szValues
  298. );
  299. DISPLAY_MESSAGE(stdout, szBuffer);
  300. if( pIPF )
  301. pIPF->Release();
  302. if( pITask )
  303. pITask->Release();
  304. // close the connection that was established by the utility
  305. if ( bCloseConnection == TRUE )
  306. CloseConnection( szServer );
  307. Cleanup(pITaskScheduler);
  308. return EXIT_SUCCESS;
  309. }
  310. // run the scheduled task immediately
  311. hr = pITask->Run();
  312. if ( FAILED(hr) )
  313. {
  314. DisplayErrorMsg(hr);
  315. if( pIPF )
  316. pIPF->Release();
  317. if( pITask )
  318. pITask->Release();
  319. // close the connection that was established by the utility
  320. if ( bCloseConnection == TRUE )
  321. CloseConnection( szServer );
  322. Cleanup(pITaskScheduler);
  323. return EXIT_FAILURE;
  324. }
  325. szValues[0] = (_TCHAR*) (wszJobName);
  326. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  327. GetResString(IDS_RUN_SUCCESSFUL),0,MAKELANGID(LANG_NEUTRAL,
  328. SUBLANG_DEFAULT),szBuffer,
  329. MAX_STRING_LENGTH,(va_list*)szValues
  330. );
  331. DISPLAY_MESSAGE(stdout,szBuffer);
  332. if( pIPF )
  333. pIPF->Release();
  334. if( pITask )
  335. pITask->Release();
  336. // close the connection that was established by the utility
  337. if ( bCloseConnection == TRUE )
  338. CloseConnection( szServer );
  339. Cleanup(pITaskScheduler);
  340. return EXIT_SUCCESS;
  341. }
  342. /*****************************************************************************
  343. Routine Description:
  344. This routine displays the usage of -run option
  345. Arguments:
  346. None
  347. Return Value :
  348. VOID
  349. ******************************************************************************/
  350. VOID
  351. DisplayRunUsage()
  352. {
  353. // Displaying run option usage
  354. DisplayUsage( IDS_RUN_HLP1, IDS_RUN_HLP16 );
  355. }