- Timestamp:
- Jul 31, 2013 2:57:04 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VBoxManage/VBoxManageGuestCtrl.cpp
r47492 r47495 202 202 { 203 203 GETOPTDEF_MKDIR_PASSWORD = 1000 204 }; 205 206 enum GETOPTDEF_SESSIONCLOSE 207 { 208 GETOPTDEF_SESSIONCLOSE_ALL = 1000 204 209 }; 205 210 … … 261 266 " list <all|sessions|processes> [--verbose]\n" 262 267 "\n" 268 " session close --session-id <ID>\n" 269 " | --session-name <name or pattern>\n" 270 " | --all\n" 271 " [--verbose]\n" 263 272 " stat\n" 264 273 " <file>... --username <name>\n" … … 2901 2910 } 2902 2911 2912 static RTEXITCODE handleCtrlSessionClose(ComPtr<IGuest> guest, HandlerArg *pArg) 2913 { 2914 AssertPtrReturn(pArg, RTEXITCODE_SYNTAX); 2915 2916 if (pArg->argc < 1) 2917 return errorSyntax(USAGE_GUESTCONTROL, "Must specify at least session ID to close"); 2918 2919 /* 2920 * Parse arguments. 2921 * 2922 * Note! No direct returns here, everyone must go thru the cleanup at the 2923 * end of this function. 2924 */ 2925 static const RTGETOPTDEF s_aOptions[] = 2926 { 2927 { "--all", GETOPTDEF_SESSIONCLOSE_ALL, RTGETOPT_REQ_NOTHING }, 2928 { "--session-id", 'i', RTGETOPT_REQ_UINT32 }, 2929 { "--session-name", 'n', RTGETOPT_REQ_UINT32 }, 2930 { "--verbose", 'v', RTGETOPT_REQ_NOTHING } 2931 }; 2932 2933 int ch; 2934 RTGETOPTUNION ValueUnion; 2935 RTGETOPTSTATE GetState; 2936 RTGetOptInit(&GetState, pArg->argc, pArg->argv, 2937 s_aOptions, RT_ELEMENTS(s_aOptions), 0, RTGETOPTINIT_FLAGS_OPTS_FIRST); 2938 2939 ULONG ulSessionID = UINT32_MAX; 2940 Utf8Str strNamePattern; 2941 bool fVerbose = false; 2942 2943 while ((ch = RTGetOpt(&GetState, &ValueUnion))) 2944 { 2945 /* For options that require an argument, ValueUnion has received the value. */ 2946 switch (ch) 2947 { 2948 case 'n': /* Session name pattern */ 2949 strNamePattern = ValueUnion.psz; 2950 break; 2951 2952 case 'i': /* Session ID */ 2953 ulSessionID = ValueUnion.u32; 2954 break; 2955 2956 /** @todo Add an option for finding all session by a name pattern. */ 2957 2958 case 'v': /* Verbose */ 2959 fVerbose = true; 2960 break; 2961 2962 case GETOPTDEF_SESSIONCLOSE_ALL: 2963 strNamePattern = "*"; 2964 break; 2965 2966 case VINF_GETOPT_NOT_OPTION: 2967 /** @todo Supply a CSV list of IDs or patterns to close? */ 2968 break; 2969 2970 default: 2971 return RTGetOptPrintError(ch, &ValueUnion); 2972 } 2973 } 2974 2975 if ( strNamePattern.isEmpty() 2976 && ulSessionID == UINT32_MAX) 2977 { 2978 return errorSyntax(USAGE_GUESTCONTROL, "No session ID specified!"); 2979 } 2980 else if ( !strNamePattern.isEmpty() 2981 && ulSessionID != UINT32_MAX) 2982 { 2983 return errorSyntax(USAGE_GUESTCONTROL, "Either session ID or name (pattern) must be specified"); 2984 } 2985 2986 HRESULT rc = S_OK; 2987 2988 ComPtr<IGuestSession> pSession; 2989 do 2990 { 2991 bool fSessionFound = false; 2992 2993 SafeIfaceArray <IGuestSession> collSessions; 2994 CHECK_ERROR_BREAK(guest, COMGETTER(Sessions)(ComSafeArrayAsOutParam(collSessions))); 2995 size_t cSessions = collSessions.size(); 2996 2997 for (size_t i = 0; i < cSessions; i++) 2998 { 2999 pSession = collSessions[i]; 3000 Assert(!pSession.isNull()); 3001 3002 ULONG uID; /* Session ID */ 3003 CHECK_ERROR_BREAK(pSession, COMGETTER(Id)(&uID)); 3004 Bstr strName; 3005 CHECK_ERROR_BREAK(pSession, COMGETTER(Name)(strName.asOutParam())); 3006 Utf8Str strNameUtf8(strName); /* Session name */ 3007 3008 if (strNamePattern.isEmpty()) /* Search by ID. Slow lookup. */ 3009 { 3010 fSessionFound = uID == ulSessionID; 3011 } 3012 else /* ... or by naming pattern. */ 3013 { 3014 if (RTStrSimplePatternMatch(strNamePattern.c_str(), strNameUtf8.c_str())) 3015 fSessionFound = true; 3016 } 3017 3018 if (fSessionFound) 3019 { 3020 Assert(!pSession.isNull()); 3021 if (fVerbose) 3022 RTPrintf("Closing guest session ID=#%RU32 \"%s\" ...\n", 3023 uID, strNameUtf8.c_str()); 3024 CHECK_ERROR_BREAK(pSession, Close()); 3025 if (fVerbose) 3026 RTPrintf("Guest session successfully closed\n"); 3027 3028 pSession->Release(); 3029 } 3030 } 3031 3032 if (!fSessionFound) 3033 { 3034 RTPrintf("No guest session(s) found\n"); 3035 rc = E_ABORT; /* To set exit code accordingly. */ 3036 } 3037 3038 } while (0); 3039 3040 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE; 3041 } 3042 3043 static RTEXITCODE handleCtrlSession(ComPtr<IGuest> guest, HandlerArg *pArg) 3044 { 3045 AssertPtrReturn(pArg, RTEXITCODE_SYNTAX); 3046 3047 if (pArg->argc < 1) 3048 return errorSyntax(USAGE_GUESTCONTROL, "Must specify an action"); 3049 3050 /** Use RTGetOpt here when handling command line args gets more complex. */ 3051 3052 HandlerArg argSub = *pArg; 3053 argSub.argc = pArg->argc - 1; /* Skip session action. */ 3054 argSub.argv = pArg->argv + 1; /* Same here. */ 3055 3056 if ( !RTStrICmp(pArg->argv[0], "close") 3057 || !RTStrICmp(pArg->argv[0], "kill") 3058 || !RTStrICmp(pArg->argv[0], "terminate")) 3059 { 3060 return handleCtrlSessionClose(guest, &argSub); 3061 } 3062 3063 return errorSyntax(USAGE_GUESTCONTROL, "Invalid session action '%s'", pArg->argv[0]); 3064 } 3065 2903 3066 /** 2904 3067 * Access the guest control store. … … 2927 3090 if (pArg->argc < 2) 2928 3091 rcExit = errorSyntax(USAGE_GUESTCONTROL, "No sub command specified!"); 2929 else if ( ! strcmp(pArg->argv[1], "exec")2930 || ! strcmp(pArg->argv[1], "execute"))3092 else if ( !RTStrICmp(pArg->argv[1], "exec") 3093 || !RTStrICmp(pArg->argv[1], "execute")) 2931 3094 rcExit = handleCtrlExecProgram(guest, &arg); 2932 else if (! strcmp(pArg->argv[1], "copyfrom"))3095 else if (!RTStrICmp(pArg->argv[1], "copyfrom")) 2933 3096 rcExit = handleCtrlCopy(guest, &arg, false /* Guest to host */); 2934 else if ( ! strcmp(pArg->argv[1], "copyto")2935 || ! strcmp(pArg->argv[1], "cp"))3097 else if ( !RTStrICmp(pArg->argv[1], "copyto") 3098 || !RTStrICmp(pArg->argv[1], "cp")) 2936 3099 rcExit = handleCtrlCopy(guest, &arg, true /* Host to guest */); 2937 else if ( ! strcmp(pArg->argv[1], "createdirectory")2938 || ! strcmp(pArg->argv[1], "createdir")2939 || ! strcmp(pArg->argv[1], "mkdir")2940 || ! strcmp(pArg->argv[1], "md"))3100 else if ( !RTStrICmp(pArg->argv[1], "createdirectory") 3101 || !RTStrICmp(pArg->argv[1], "createdir") 3102 || !RTStrICmp(pArg->argv[1], "mkdir") 3103 || !RTStrICmp(pArg->argv[1], "md")) 2941 3104 rcExit = handleCtrlCreateDirectory(guest, &arg); 2942 else if ( ! strcmp(pArg->argv[1], "createtemporary")2943 || ! strcmp(pArg->argv[1], "createtemp")2944 || ! strcmp(pArg->argv[1], "mktemp"))3105 else if ( !RTStrICmp(pArg->argv[1], "createtemporary") 3106 || !RTStrICmp(pArg->argv[1], "createtemp") 3107 || !RTStrICmp(pArg->argv[1], "mktemp")) 2945 3108 rcExit = handleCtrlCreateTemp(guest, &arg); 2946 else if ( ! strcmp(pArg->argv[1], "stat"))3109 else if ( !RTStrICmp(pArg->argv[1], "stat")) 2947 3110 rcExit = handleCtrlStat(guest, &arg); 2948 else if ( ! strcmp(pArg->argv[1], "updateadditions")2949 || ! strcmp(pArg->argv[1], "updateadds"))3111 else if ( !RTStrICmp(pArg->argv[1], "updateadditions") 3112 || !RTStrICmp(pArg->argv[1], "updateadds")) 2950 3113 rcExit = handleCtrlUpdateAdditions(guest, &arg); 2951 else if ( ! strcmp(pArg->argv[1], "list"))3114 else if ( !RTStrICmp(pArg->argv[1], "list")) 2952 3115 rcExit = handleCtrlList(guest, &arg); 3116 else if ( !RTStrICmp(pArg->argv[1], "session")) 3117 rcExit = handleCtrlSession(guest, &arg); 2953 3118 else 2954 3119 rcExit = errorSyntax(USAGE_GUESTCONTROL, "Unknown sub command '%s' specified!", pArg->argv[1]);
Note:
See TracChangeset
for help on using the changeset viewer.