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.

404 lines
9.3 KiB

  1. /******************************************************************************
  2. Copyright(c) Microsoft Corporation
  3. Module Name:
  4. end.cpp
  5. Abstract:
  6. This module terminates the schedule task which is currently running 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 DisplayEndUsage();
  17. /*****************************************************************************
  18. Routine Description:
  19. This routine terminates 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. TerminateScheduledTask( DWORD argc, LPCTSTR argv[] )
  29. {
  30. // Variables used to find whether End option, Usage option
  31. // are specified or not
  32. BOOL bEnd = 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. BOOL bNeedPassword = FALSE;
  50. BOOL bResult = FALSE;
  51. BOOL bCloseConnection = TRUE;
  52. //buffer for displaying error message
  53. TCHAR szMessage[MAX_STRING_LENGTH] = NULL_STRING;
  54. lstrcpy( szPassword, ASTERIX);
  55. // Builiding the TCMDPARSER structure
  56. TCMDPARSER cmdOptions[] =
  57. {
  58. {
  59. CMDOPTION_END,
  60. CP_MAIN_OPTION,
  61. 1,
  62. 0,
  63. &bEnd,
  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 end 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_END_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_ENDPASSWORD].dwActuals == 0 )
  132. {
  133. lstrcpy( szPassword, NULL_STRING );
  134. }
  135. // Displaying end usage if user specified -? with -run option
  136. if( bUsage == TRUE )
  137. {
  138. DisplayEndUsage();
  139. return EXIT_SUCCESS;
  140. }
  141. // check for invalid user name
  142. if( ( cmdOptions[OI_ENDSERVER].dwActuals == 0 ) && ( cmdOptions[OI_ENDUSERNAME].dwActuals == 1 ) )
  143. {
  144. DISPLAY_MESSAGE(stderr, GetResString(IDS_END_USER_BUT_NOMACHINE));
  145. return RETVAL_FAIL;
  146. }
  147. // check for the length of user name
  148. if( ( cmdOptions[OI_ENDSERVER].dwActuals == 1 ) && ( cmdOptions[OI_ENDUSERNAME].dwActuals == 1 ) &&
  149. ( lstrlen( szUser ) == 0 ) )
  150. {
  151. DISPLAY_MESSAGE(stderr, GetResString(IDS_INVALID_USERNAME));
  152. return RETVAL_FAIL;
  153. }
  154. // check for invalid server name
  155. if( ( cmdOptions[OI_ENDSERVER].dwActuals == 1 ) && ( lstrlen( szServer ) == 0 ) )
  156. {
  157. DISPLAY_MESSAGE(stderr, GetResString(IDS_NO_SERVER));
  158. return RETVAL_FAIL;
  159. }
  160. // check for invalid username
  161. if ( cmdOptions[ OI_ENDUSERNAME ].dwActuals == 0 && cmdOptions[ OI_ENDPASSWORD ].dwActuals == 1 )
  162. {
  163. // invalid syntax
  164. DISPLAY_MESSAGE(stderr, GetResString(IDS_EPASSWORD_BUT_NOUSERNAME));
  165. return RETVAL_FAIL; // indicate failure
  166. }
  167. // check for the length of the taskname
  168. if( ( lstrlen( szTaskName ) > MAX_JOB_LEN ) || ( lstrlen(szTaskName) == 0 ) )
  169. {
  170. DISPLAY_MESSAGE(stderr, GetResString(IDS_INVALID_TASKLENGTH));
  171. return RETVAL_FAIL;
  172. }
  173. // Convert the task name specified by the user to wide char or unicode format
  174. if ( GetAsUnicodeString(szTaskName,wszJobName,SIZE_OF_ARRAY(wszJobName)) == NULL )
  175. {
  176. return RETVAL_FAIL;
  177. }
  178. // check for the length of the username
  179. if( lstrlen( szUser ) > MAX_USERNAME_LENGTH )
  180. {
  181. DISPLAY_MESSAGE( stderr, GetResString(IDS_INVALID_UNAME) );
  182. return RETVAL_FAIL;
  183. }
  184. // check for the length of the password
  185. if( lstrlen( szPassword ) > MAX_PASSWORD_LENGTH )
  186. {
  187. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_PASSWORD));
  188. return RETVAL_FAIL;
  189. }
  190. //for holding values of parameters in FormatMessage()
  191. _TCHAR* szValues[1] = {NULL};
  192. // check whether the password (-p) specified in the command line or not
  193. // and also check whether '*' or empty is given for -p or not
  194. if( ( IsLocalSystem( szServer ) == FALSE ) &&
  195. ( ( cmdOptions[OI_ENDPASSWORD].dwActuals == 0 ) || ( lstrcmpi ( szPassword, ASTERIX ) == 0 ) ) )
  196. {
  197. bNeedPassword = TRUE;
  198. }
  199. if( ( IsLocalSystem( szServer ) == FALSE ) || ( cmdOptions[OI_ENDUSERNAME].dwActuals == 1 ))
  200. {
  201. // Establish the connection on a remote machine
  202. bResult = EstablishConnection(szServer,szUser,SIZE_OF_ARRAY(szUser),szPassword,SIZE_OF_ARRAY(szPassword), bNeedPassword);
  203. if (bResult == FALSE)
  204. {
  205. DISPLAY_MESSAGE( stderr, GetResString(IDS_ERROR_STRING) );
  206. DISPLAY_MESSAGE( stderr, GetReason());
  207. return EXIT_FAILURE ;
  208. }
  209. else
  210. {
  211. // though the connection is successfull, some conflict might have occured
  212. switch( GetLastError() )
  213. {
  214. case I_NO_CLOSE_CONNECTION:
  215. bCloseConnection = FALSE;
  216. break;
  217. case E_LOCAL_CREDENTIALS:
  218. case ERROR_SESSION_CREDENTIAL_CONFLICT:
  219. {
  220. bCloseConnection = FALSE;
  221. DISPLAY_MESSAGE( stderr, GetResString(IDS_ERROR_STRING) );
  222. DISPLAY_MESSAGE( stderr, GetReason());
  223. return EXIT_FAILURE;
  224. }
  225. }
  226. }
  227. }
  228. // Get the task Scheduler object for the machine.
  229. pITaskScheduler = GetTaskScheduler( szServer );
  230. // If the Task Scheduler is not defined then give the error message.
  231. if ( pITaskScheduler == NULL )
  232. {
  233. // close the connection that was established by the utility
  234. if ( bCloseConnection == TRUE )
  235. CloseConnection( szServer );
  236. Cleanup(pITaskScheduler);
  237. return EXIT_FAILURE;
  238. }
  239. // Validate the Given Task and get as TARRAY in case of taskname
  240. arrJobs = ValidateAndGetTasks( pITaskScheduler, szTaskName);
  241. if( arrJobs == NULL )
  242. {
  243. _stprintf( szMessage , GetResString(IDS_TASKNAME_NOTEXIST), szTaskName);
  244. DISPLAY_MESSAGE(stderr,szMessage);
  245. // close the connection that was established by the utility
  246. if ( bCloseConnection == TRUE )
  247. CloseConnection( szServer );
  248. Cleanup(pITaskScheduler);
  249. return EXIT_FAILURE;
  250. }
  251. IPersistFile *pIPF = NULL;
  252. ITask *pITask = NULL;
  253. // return an pITask inteface for wszJobName
  254. hr = pITaskScheduler->Activate(wszJobName,IID_ITask,
  255. (IUnknown**) &pITask);
  256. if (FAILED(hr))
  257. {
  258. DisplayErrorMsg(hr);
  259. if( pIPF )
  260. pIPF->Release();
  261. if( pITask )
  262. pITask->Release();
  263. // close the connection that was established by the utility
  264. if ( bCloseConnection == TRUE )
  265. CloseConnection( szServer );
  266. Cleanup(pITaskScheduler);
  267. return EXIT_FAILURE;
  268. }
  269. DWORD dwResult = 0;
  270. ULONG ulLong = MAX_RES_STRING;
  271. TCHAR szBuffer[MAX_STRING_LENGTH] = NULL_STRING;
  272. // terminate the scheduled task
  273. hr = pITask->Terminate();
  274. if ( FAILED(hr) )
  275. {
  276. DisplayErrorMsg(hr);
  277. if( pIPF )
  278. pIPF->Release();
  279. if( pITask )
  280. pITask->Release();
  281. // close the connection that was established by the utility
  282. if ( bCloseConnection == TRUE )
  283. CloseConnection( szServer );
  284. Cleanup(pITaskScheduler);
  285. return EXIT_FAILURE;
  286. }
  287. else
  288. {
  289. szValues[0] = (_TCHAR*) (wszJobName);
  290. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  291. GetResString(IDS_END_SUCCESSFUL),0,MAKELANGID(LANG_NEUTRAL,
  292. SUBLANG_DEFAULT),szBuffer,
  293. MAX_STRING_LENGTH,(va_list*)szValues
  294. );
  295. DISPLAY_MESSAGE(stdout,szBuffer);
  296. }
  297. if( pIPF )
  298. pIPF->Release();
  299. if( pITask )
  300. pITask->Release();
  301. // close the connection that was established by the utility
  302. if ( bCloseConnection == TRUE )
  303. CloseConnection( szServer );
  304. Cleanup(pITaskScheduler);
  305. return EXIT_SUCCESS;
  306. }
  307. /*****************************************************************************
  308. Routine Description:
  309. This routine displays the usage of -end option
  310. Arguments:
  311. None
  312. Return Value :
  313. VOID
  314. ******************************************************************************/
  315. VOID
  316. DisplayEndUsage()
  317. {
  318. // Displaying run option usage
  319. DisplayUsage( IDS_END_HLP1, IDS_END_HLP17);
  320. }