rtProcInitExePath-freebsd.cpp revision 92453c7e11969eccff568c08178524f057247426
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * IPRT - rtProcInitName, FreeBSD.
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * Copyright (C) 2006-2008 Oracle Corporation
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * available from http://www.virtualbox.org. This file is free software;
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * General Public License (GPL) as published by the Free Software
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * The contents of this file may alternatively be used under the terms
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * of the Common Development and Distribution License Version 1.0
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * VirtualBox OSE distribution, in which case the provisions of the
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * CDDL are applicable instead of those of the GPL.
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * You may elect to license modified versions of this file under the
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * terms and conditions of either the GPL or the CDDL or both.
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync/*******************************************************************************
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync* Header Files *
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync*******************************************************************************/
404df8b2baf3f931a57bffd773b093d0fe40cd50vboxsyncDECLHIDDEN(int) rtProcInitExePath(char *pszPath, size_t cchPath)
404df8b2baf3f931a57bffd773b093d0fe40cd50vboxsync aiName[2] = KERN_PROC_PATHNAME; /* This was introduced in FreeBSD 6.0, thus the #ifdef above. */
404df8b2baf3f931a57bffd773b093d0fe40cd50vboxsync aiName[3] = -1; /* Shorthand for the current process. */
404df8b2baf3f931a57bffd773b093d0fe40cd50vboxsync if (sysctl(aiName, RT_ELEMENTS(aiName), pszPath, &cchExePath, NULL, 0) == 0)
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync const char *pszTmp;
f6f1d32779a2a6a1a4579c3bf6ff3aa0c37f0823vboxsync AssertMsgRCReturn(rc, ("rc=%Rrc pszPath=\"%s\"\nhex: %.*Rhxs\n", rc, pszPath, cchExePath, pszPath), rc);
f6f1d32779a2a6a1a4579c3bf6ff3aa0c37f0823vboxsync AssertMsgFailed(("rc=%Rrc errno=%d cchExePath=%d\n", rc, errno, cchExePath));
f6f1d32779a2a6a1a4579c3bf6ff3aa0c37f0823vboxsync * Read the /proc/curproc/file link, convert to native and return it.
f6f1d32779a2a6a1a4579c3bf6ff3aa0c37f0823vboxsync int cchLink = readlink("/proc/curproc/file", pszPath, cchPath - 1);
f6f1d32779a2a6a1a4579c3bf6ff3aa0c37f0823vboxsync char const *pszTmp;
f6f1d32779a2a6a1a4579c3bf6ff3aa0c37f0823vboxsync AssertMsgRCReturn(rc, ("rc=%Rrc pszLink=\"%s\"\nhex: %.*Rhxs\n", rc, pszPath, cchLink, pszPath), rc);
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * Fall back on the dynamic linker since /proc is optional.
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync if (*pszImageName == '/') /* this may not always be absolute, despite the docs. :-( */
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync char const *pszTmp;
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync int rc = rtPathFromNative(&pszTmp, pszImageName, NULL);
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync AssertMsgRCReturn(rc, ("rc=%Rrc pszImageName=\"%s\"\n", rc, pszImageName), rc);
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync /** @todo Try search the PATH for the file name or append the current
4e38f78b4916ef128a3902ed2a8d7e8d7f947d8dvboxsync * directory, which ever makes sense... */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync AssertMsgFailed(("rc=%Rrc err=%d cchLink=%d hExe=%p\n", rc, err, cchLink, hExe));