f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync/* $Id$ */
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync/** @file
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync * IPRT - rtProcInitName, FreeBSD.
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync */
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync/*
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2010 Oracle Corporation
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
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync/*******************************************************************************
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync* Header Files *
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync*******************************************************************************/
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync#define LOG_GROUP RTLOGGROUP_PROCESS
240493c14bce20febf913bd0546d2302d651c3e8vboxsync#include <sys/param.h>
240493c14bce20febf913bd0546d2302d651c3e8vboxsync#include <sys/sysctl.h>
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync#include <unistd.h>
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync#include <errno.h>
6c9516a1c3792e630f7947ceea85a21286e36d3dvboxsync#include <dlfcn.h>
6c9516a1c3792e630f7947ceea85a21286e36d3dvboxsync#include <link.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{
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync#ifdef KERN_PROC_PATHNAME
240493c14bce20febf913bd0546d2302d651c3e8vboxsync int aiName[4];
240493c14bce20febf913bd0546d2302d651c3e8vboxsync aiName[0] = CTL_KERN;
240493c14bce20febf913bd0546d2302d651c3e8vboxsync aiName[1] = KERN_PROC;
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync aiName[2] = KERN_PROC_PATHNAME; /* This was introduced in FreeBSD 6.0, thus the #ifdef above. */
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync aiName[3] = -1; /* Shorthand for the current process. */
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync size_t cchExePath = cchPath;
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync if (sysctl(aiName, RT_ELEMENTS(aiName), pszPath, &cchExePath, NULL, 0) == 0)
240493c14bce20febf913bd0546d2302d651c3e8vboxsync {
95bbd60a323feec1eaf008a93690364eea86c298vboxsync const char *pszTmp;
95bbd60a323feec1eaf008a93690364eea86c298vboxsync int rc = rtPathFromNative(&pszTmp, pszPath, NULL);
92453c7e11969eccff568c08178524f057247426vboxsync AssertMsgRCReturn(rc, ("rc=%Rrc pszPath=\"%s\"\nhex: %.*Rhxs\n", rc, pszPath, cchExePath, pszPath), rc);
95bbd60a323feec1eaf008a93690364eea86c298vboxsync if (pszTmp != pszPath)
95bbd60a323feec1eaf008a93690364eea86c298vboxsync {
95bbd60a323feec1eaf008a93690364eea86c298vboxsync rc = RTStrCopy(pszPath, cchPath, pszTmp);
95bbd60a323feec1eaf008a93690364eea86c298vboxsync rtPathFreeIprt(pszTmp, pszPath);
95bbd60a323feec1eaf008a93690364eea86c298vboxsync }
95bbd60a323feec1eaf008a93690364eea86c298vboxsync return rc;
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync }
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync
240493c14bce20febf913bd0546d2302d651c3e8vboxsync int rc = RTErrConvertFromErrno(errno);
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync AssertMsgFailed(("rc=%Rrc errno=%d cchExePath=%d\n", rc, errno, cchExePath));
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync return rc;
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync#else
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync /*
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync * Read the /proc/curproc/file link, convert to native and return it.
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync */
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync int cchLink = readlink("/proc/curproc/file", pszPath, cchPath - 1);
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync if (cchLink > 0 && (size_t)cchLink <= cchPath - 1)
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync {
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync pszPath[cchLink] = '\0';
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync
95bbd60a323feec1eaf008a93690364eea86c298vboxsync char const *pszTmp;
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync int rc = rtPathFromNative(&pszTmp, pszPath);
92453c7e11969eccff568c08178524f057247426vboxsync AssertMsgRCReturn(rc, ("rc=%Rrc pszLink=\"%s\"\nhex: %.*Rhxs\n", rc, pszPath, cchLink, pszPath), rc);
95bbd60a323feec1eaf008a93690364eea86c298vboxsync if (pszTmp != pszPath)
95bbd60a323feec1eaf008a93690364eea86c298vboxsync {
95bbd60a323feec1eaf008a93690364eea86c298vboxsync rc = RTStrCopy(pszPath, cchPath, pszTmp);
95bbd60a323feec1eaf008a93690364eea86c298vboxsync rtPathFreeIprt(pszTmp);
95bbd60a323feec1eaf008a93690364eea86c298vboxsync }
95bbd60a323feec1eaf008a93690364eea86c298vboxsync return rc;
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync }
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync int err = errno;
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync /*
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync * Fall back on the dynamic linker since /proc is optional.
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync */
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync void *hExe = dlopen(NULL, 0);
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync if (hExe)
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync {
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync struct link_map const *pLinkMap = 0;
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync if (dlinfo(hExe, RTLD_DI_LINKMAP, &pLinkMap) == 0)
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync {
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync const char *pszImageName = pLinkMap->l_name;
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync if (*pszImageName == '/') /* this may not always be absolute, despite the docs. :-( */
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync {
95bbd60a323feec1eaf008a93690364eea86c298vboxsync char const *pszTmp;
95bbd60a323feec1eaf008a93690364eea86c298vboxsync int rc = rtPathFromNative(&pszTmp, pszImageName, NULL);
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync AssertMsgRCReturn(rc, ("rc=%Rrc pszImageName=\"%s\"\n", rc, pszImageName), rc);
95bbd60a323feec1eaf008a93690364eea86c298vboxsync if (pszTmp != pszPath)
95bbd60a323feec1eaf008a93690364eea86c298vboxsync {
95bbd60a323feec1eaf008a93690364eea86c298vboxsync rc = RTStrCopy(pszPath, cchPath, pszTmp);
95bbd60a323feec1eaf008a93690364eea86c298vboxsync rtPathFreeIprt(pszTmp, pszPath);
95bbd60a323feec1eaf008a93690364eea86c298vboxsync }
95bbd60a323feec1eaf008a93690364eea86c298vboxsync return rc;
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync }
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync /** @todo Try search the PATH for the file name or append the current
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync * directory, which ever makes sense... */
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync }
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync }
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync int rc = RTErrConvertFromErrno(err);
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync AssertMsgFailed(("rc=%Rrc err=%d cchLink=%d hExe=%p\n", rc, err, cchLink, hExe));
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync return rc;
cbc4fdcbcaa06e2fa339e863429f05e9f537b0e9vboxsync#endif
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync}
f0f603c5fba5a342c00101bbaf5fad0bbd44fb55vboxsync