rtProcInitExePath-solaris.cpp revision 0691cad6d9b955fe3c5ad03f31fc399bfd154398
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync/* $Id$ */
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync/** @file
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * IPRT - rtProcInitName, Solaris.
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync */
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync/*
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * Copyright (C) 2006-2008 Sun Microsystems, Inc.
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync *
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * available from http://www.virtualbox.org. This file is free software;
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * you can redistribute it and/or modify it under the terms of the GNU
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * General Public License (GPL) as published by the Free Software
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync *
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * The contents of this file may alternatively be used under the terms
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * of the Common Development and Distribution License Version 1.0
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * VirtualBox OSE distribution, in which case the provisions of the
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * CDDL are applicable instead of those of the GPL.
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync *
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * You may elect to license modified versions of this file under the
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * terms and conditions of either the GPL or the CDDL or both.
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync *
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * additional information or have any questions.
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync */
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync/*******************************************************************************
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync* Header Files *
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync*******************************************************************************/
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync#define LOG_GROUP RTLOGGROUP_PROCESS
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync#include <unistd.h>
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync#include <errno.h>
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync#include <iprt/string.h>
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync#include <iprt/assert.h>
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync#include <iprt/err.h>
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync#include <iprt/path.h>
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync#include "internal/process.h"
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync#include "internal/path.h"
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsyncDECLHIDDEN(int) rtProcInitExePath(char *pszPath, size_t cchPath)
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync{
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync /*
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * Read the /proc/<pid>/path/a.out link, convert to native and return it.
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync */
0691cad6d9b955fe3c5ad03f31fc399bfd154398vboxsync char szProcFile[80];
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync RTStrPrintf(szProcFile, sizeof(szProcFile), "/proc/%ld/path/a.out", (long)getpid());
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync int cchLink = readlink(szProcFile, pszPath, cchPath - 1);
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync if (cchLink > 0 && (size_t)cchLink <= cchPath - 1)
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync {
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync pszPath[cchLink] = '\0';
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync char *pszTmp = NULL;
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync int rc = rtPathFromNative(&pszTmp, pszPath);
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync AssertMsgRCReturn(rc, ("rc=%Rrc pszLink=\"%s\"\nhex: %.*Rhsx\n", rc, pszPath, cchLink, pszPath), rc);
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync size_t cch = strlen(pszTmp);
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync AssertReturn(cch <= cchPath, VERR_BUFFER_OVERFLOW);
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync memcpy(pszPath, pszTmp, cch + 1);
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync RTStrFree(pszTmp);
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync return VINF_SUCCESS;
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync }
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync int err = errno;
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync int rc = RTErrConvertFromErrno(err);
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync AssertMsgFailed(("rc=%Rrc err=%d cchLink=%d\n", rc, err, cchLink));
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync return rc;
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync}
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync