1 | /* $Id: rtProcInitExePath-freebsd.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - rtProcInitName, FreeBSD.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #define LOG_GROUP RTLOGGROUP_PROCESS
|
---|
42 | #include <sys/param.h>
|
---|
43 | #include <sys/sysctl.h>
|
---|
44 | #include <unistd.h>
|
---|
45 | #include <errno.h>
|
---|
46 | #include <dlfcn.h>
|
---|
47 | #include <link.h>
|
---|
48 |
|
---|
49 | #include <iprt/string.h>
|
---|
50 | #include <iprt/assert.h>
|
---|
51 | #include <iprt/errcore.h>
|
---|
52 | #include <iprt/path.h>
|
---|
53 | #include "internal/process.h"
|
---|
54 | #include "internal/path.h"
|
---|
55 |
|
---|
56 |
|
---|
57 | DECLHIDDEN(int) rtProcInitExePath(char *pszPath, size_t cchPath)
|
---|
58 | {
|
---|
59 | #ifdef KERN_PROC_PATHNAME
|
---|
60 | int aiName[4];
|
---|
61 | aiName[0] = CTL_KERN;
|
---|
62 | aiName[1] = KERN_PROC;
|
---|
63 | aiName[2] = KERN_PROC_PATHNAME; /* This was introduced in FreeBSD 6.0, thus the #ifdef above. */
|
---|
64 | aiName[3] = -1; /* Shorthand for the current process. */
|
---|
65 |
|
---|
66 | size_t cchExePath = cchPath;
|
---|
67 | if (sysctl(aiName, RT_ELEMENTS(aiName), pszPath, &cchExePath, NULL, 0) == 0)
|
---|
68 | {
|
---|
69 | const char *pszTmp;
|
---|
70 | int rc = rtPathFromNative(&pszTmp, pszPath, NULL);
|
---|
71 | AssertMsgRCReturn(rc, ("rc=%Rrc pszPath=\"%s\"\nhex: %.*Rhxs\n", rc, pszPath, cchExePath, pszPath), rc);
|
---|
72 | if (pszTmp != pszPath)
|
---|
73 | {
|
---|
74 | rc = RTStrCopy(pszPath, cchPath, pszTmp);
|
---|
75 | rtPathFreeIprt(pszTmp, pszPath);
|
---|
76 | }
|
---|
77 | return rc;
|
---|
78 | }
|
---|
79 |
|
---|
80 | int rc = RTErrConvertFromErrno(errno);
|
---|
81 | AssertMsgFailed(("rc=%Rrc errno=%d cchExePath=%d\n", rc, errno, cchExePath));
|
---|
82 | return rc;
|
---|
83 |
|
---|
84 | #else
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Read the /proc/curproc/file link, convert to native and return it.
|
---|
88 | */
|
---|
89 | int cchLink = readlink("/proc/curproc/file", pszPath, cchPath - 1);
|
---|
90 | if (cchLink > 0 && (size_t)cchLink <= cchPath - 1)
|
---|
91 | {
|
---|
92 | pszPath[cchLink] = '\0';
|
---|
93 |
|
---|
94 | char const *pszTmp;
|
---|
95 | int rc = rtPathFromNative(&pszTmp, pszPath);
|
---|
96 | AssertMsgRCReturn(rc, ("rc=%Rrc pszLink=\"%s\"\nhex: %.*Rhxs\n", rc, pszPath, cchLink, pszPath), rc);
|
---|
97 | if (pszTmp != pszPath)
|
---|
98 | {
|
---|
99 | rc = RTStrCopy(pszPath, cchPath, pszTmp);
|
---|
100 | rtPathFreeIprt(pszTmp);
|
---|
101 | }
|
---|
102 | return rc;
|
---|
103 | }
|
---|
104 |
|
---|
105 | int err = errno;
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * Fall back on the dynamic linker since /proc is optional.
|
---|
109 | */
|
---|
110 | void *hExe = dlopen(NULL, 0);
|
---|
111 | if (hExe)
|
---|
112 | {
|
---|
113 | struct link_map const *pLinkMap = 0;
|
---|
114 | if (dlinfo(hExe, RTLD_DI_LINKMAP, &pLinkMap) == 0)
|
---|
115 | {
|
---|
116 | const char *pszImageName = pLinkMap->l_name;
|
---|
117 | if (*pszImageName == '/') /* this may not always be absolute, despite the docs. :-( */
|
---|
118 | {
|
---|
119 | char const *pszTmp;
|
---|
120 | int rc = rtPathFromNative(&pszTmp, pszImageName, NULL);
|
---|
121 | AssertMsgRCReturn(rc, ("rc=%Rrc pszImageName=\"%s\"\n", rc, pszImageName), rc);
|
---|
122 | if (pszTmp != pszPath)
|
---|
123 | {
|
---|
124 | rc = RTStrCopy(pszPath, cchPath, pszTmp);
|
---|
125 | rtPathFreeIprt(pszTmp, pszPath);
|
---|
126 | }
|
---|
127 | return rc;
|
---|
128 | }
|
---|
129 | /** @todo Try search the PATH for the file name or append the current
|
---|
130 | * directory, which ever makes sense... */
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | int rc = RTErrConvertFromErrno(err);
|
---|
135 | AssertMsgFailed(("rc=%Rrc err=%d cchLink=%d hExe=%p\n", rc, err, cchLink, hExe));
|
---|
136 | return rc;
|
---|
137 | #endif
|
---|
138 | }
|
---|
139 |
|
---|