rtProcInitExePath-linux.cpp revision 95bbd60a323feec1eaf008a93690364eea86c298
af062818b47340eef15700d2f0211576ba3506eevboxsync/* $Id$ */
af062818b47340eef15700d2f0211576ba3506eevboxsync/** @file
af062818b47340eef15700d2f0211576ba3506eevboxsync * IPRT - rtProcInitName, Linux.
af062818b47340eef15700d2f0211576ba3506eevboxsync */
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync/*
af062818b47340eef15700d2f0211576ba3506eevboxsync * Copyright (C) 2006-2008 Oracle Corporation
af062818b47340eef15700d2f0211576ba3506eevboxsync *
af062818b47340eef15700d2f0211576ba3506eevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
af062818b47340eef15700d2f0211576ba3506eevboxsync * available from http://www.virtualbox.org. This file is free software;
af062818b47340eef15700d2f0211576ba3506eevboxsync * you can redistribute it and/or modify it under the terms of the GNU
af062818b47340eef15700d2f0211576ba3506eevboxsync * General Public License (GPL) as published by the Free Software
af062818b47340eef15700d2f0211576ba3506eevboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
af062818b47340eef15700d2f0211576ba3506eevboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
af062818b47340eef15700d2f0211576ba3506eevboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
af062818b47340eef15700d2f0211576ba3506eevboxsync *
af062818b47340eef15700d2f0211576ba3506eevboxsync * The contents of this file may alternatively be used under the terms
af062818b47340eef15700d2f0211576ba3506eevboxsync * of the Common Development and Distribution License Version 1.0
af062818b47340eef15700d2f0211576ba3506eevboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
ee6bcfc59fe3b0230aad85e2ef63d0402b7719b2vboxsync * VirtualBox OSE distribution, in which case the provisions of the
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * CDDL are applicable instead of those of the GPL.
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync *
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * You may elect to license modified versions of this file under the
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * terms and conditions of either the GPL or the CDDL or both.
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync */
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync/*******************************************************************************
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync* Header Files *
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync*******************************************************************************/
af062818b47340eef15700d2f0211576ba3506eevboxsync#define LOG_GROUP RTLOGGROUP_PROCESS
af062818b47340eef15700d2f0211576ba3506eevboxsync#include <unistd.h>
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync#include <errno.h>
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync#include <iprt/string.h>
af062818b47340eef15700d2f0211576ba3506eevboxsync#include <iprt/assert.h>
af062818b47340eef15700d2f0211576ba3506eevboxsync#include <iprt/err.h>
af062818b47340eef15700d2f0211576ba3506eevboxsync#include <iprt/path.h>
af062818b47340eef15700d2f0211576ba3506eevboxsync#include "internal/process.h"
af062818b47340eef15700d2f0211576ba3506eevboxsync#include "internal/path.h"
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsyncDECLHIDDEN(int) rtProcInitExePath(char *pszPath, size_t cchPath)
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync{
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync /*
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync * Read the /proc/self/exe link, convert to native and return it.
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync */
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync int cchLink = readlink("/proc/self/exe", pszPath, cchPath - 1);
af062818b47340eef15700d2f0211576ba3506eevboxsync if (cchLink > 0 && (size_t)cchLink <= cchPath - 1)
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync pszPath[cchLink] = '\0';
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync char const *pszTmp;
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync int rc = rtPathFromNative(&pszTmp, pszPath, NULL);
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync AssertMsgRCReturn(rc, ("rc=%Rrc pszLink=\"%s\"\nhex: %.*Rhsx\n", rc, pszPath, cchLink, pszPath), rc);
af062818b47340eef15700d2f0211576ba3506eevboxsync if (pszTmp != pszPath)
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync rc = RTStrCopy(pszPath, cchPath, pszTmp);
af062818b47340eef15700d2f0211576ba3506eevboxsync rtPathFreeIprt(pszTmp, pszPath);
}
return rc;
}
int err = errno;
int rc = RTErrConvertFromErrno(err);
AssertMsgFailed(("rc=%Rrc err=%d cchLink=%d\n", rc, err, cchLink));
return rc;
}