RTSystemShutdown-solaris.cpp revision 176e5972d88cf5575c6854860310aff1f3ddfab6
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync/* $Id$ */
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync/** @file
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * IPRT - RTSystemShutdown, linux implementation.
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync */
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync/*
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * Copyright (C) 2012 Oracle Corporation
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync *
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * available from http://www.virtualbox.org. This file is free software;
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * you can redistribute it and/or modify it under the terms of the GNU
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * General Public License (GPL) as published by the Free Software
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync *
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * The contents of this file may alternatively be used under the terms
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * of the Common Development and Distribution License Version 1.0
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * VirtualBox OSE distribution, in which case the provisions of the
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * CDDL are applicable instead of those of the GPL.
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync *
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * You may elect to license modified versions of this file under the
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * terms and conditions of either the GPL or the CDDL or both.
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync */
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync/*******************************************************************************
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync* Header Files *
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync*******************************************************************************/
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync#include <iprt/system.h>
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync#include "internal/iprt.h"
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync#include <iprt/assert.h>
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync#include <iprt/env.h>
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync#include <iprt/err.h>
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync#include <iprt/process.h>
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync#include <iprt/string.h>
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsyncRTDECL(int) RTSystemShutdown(RTMSINTERVAL cMsDelay, uint32_t fFlags, const char *pszLogMsg)
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync{
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync AssertPtrReturn(pszLogMsg, VERR_INVALID_POINTER);
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync AssertReturn(!(fFlags & ~RTSYSTEM_SHUTDOWN_VALID_MASK), VERR_INVALID_PARAMETER);
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync /*
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync * Assemble the argument vector.
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync */
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync int iArg = 0;
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync const char *apszArgs[8];
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync RT_BZERO(apszArgs, sizeof(apszArgs));
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync apszArgs[iArg++] = "/usr/sbin/shutdown";
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync apszArgs[iArg++] = "-y"; /* Pre-answer confirmation question. */
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync apszArgs[iArg++] = "-i"; /* Change to the following state. */
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync switch (fFlags & RTSYSTEM_SHUTDOWN_ACTION_MASK)
3f8d7e373b73cca71b1889377a3eea5d3362a01bvboxsync {
case RTSYSTEM_SHUTDOWN_HALT:
apszArgs[iArg++] = "0";
break;
case RTSYSTEM_SHUTDOWN_REBOOT:
apszArgs[iArg++] = "6";
break;
case RTSYSTEM_SHUTDOWN_POWER_OFF:
case RTSYSTEM_SHUTDOWN_POWER_OFF_HALT:
apszArgs[iArg++] = "5";
break;
}
apszArgs[iArg++] = "-g"; /* Grace period. */
char szWhen[80];
if (cMsDelay < 500)
strcpy(szWhen, "0");
else
RTStrPrintf(szWhen, sizeof(szWhen), "%u", (unsigned)((cMsDelay + 499) / 1000));
apszArgs[iArg++] = szWhen;
apszArgs[iArg++] = pszLogMsg;
/*
* Start the shutdown process and wait for it to complete.
*/
RTPROCESS hProc;
int rc = RTProcCreate(apszArgs[0], apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hProc);
if (RT_FAILURE(rc))
return rc;
RTPROCSTATUS ProcStatus;
rc = RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus);
if (RT_SUCCESS(rc))
{
if ( ProcStatus.enmReason != RTPROCEXITREASON_NORMAL
|| ProcStatus.iStatus != 0)
rc = VERR_SYS_SHUTDOWN_FAILED;
}
return rc;
}