BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdls_processutil.h
Go to the documentation of this file.
1/// @file bdls_processutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdls_processutil.h -*-C++-*-
8#ifndef INCLUDED_BDLS_PROCESSUTIL
9#define INCLUDED_BDLS_PROCESSUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdls_processutil bdls_processutil
15/// @brief Provide basic platform-independent utilities related to processes.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdls
19/// @{
20/// @addtogroup bdls_processutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdls_processutil-purpose"> Purpose</a>
25/// * <a href="#bdls_processutil-classes"> Classes </a>
26/// * <a href="#bdls_processutil-description"> Description </a>
27/// * <a href="#bdls_processutil-getprocessname-vs-getpathtoexecutable"> getProcessName vs getPathToExecutable </a>
28/// * <a href="#bdls_processutil-usage"> Usage </a>
29/// * <a href="#bdls_processutil-example-1-basic-usage"> Example 1: Basic Usage </a>
30///
31/// # Purpose {#bdls_processutil-purpose}
32/// Provide basic platform-independent utilities related to processes.
33///
34/// # Classes {#bdls_processutil-classes}
35///
36/// - bdls::ProcessUtil: portable utility methods related to processes
37///
38/// @see bdls_filesystemutil
39///
40/// # Description {#bdls_processutil-description}
41/// This component, `bdls::ProcessUtil`, defines a
42/// platform-independent interface for processes. Currently, it provides a
43/// utility to get the current process ID, a utility to get the name of the
44/// process, and a utility to get a filename through which the executable can be
45/// accessed.
46///
47/// ## getProcessName vs getPathToExecutable {#bdls_processutil-getprocessname-vs-getpathtoexecutable}
48///
49///
50/// The `getProcessName` function is intended to yield a process name that will
51/// be meaningful to the programmer, recognizably related to the value of
52/// `argv[0]` passed to the application, and usually a valid path to the
53/// executable file. However, in some cases, especially when `argv[0]` was a
54/// relative path and the working directory has changed since `main` was called,
55/// this will not be a usable path for accessing the executable file, and
56/// `getPathToExecutable` should be used. Note that the return value of
57/// `getPathToExecutable` may be completely unrelated to the value of `argv[0]`
58/// passed to main, and the value returned by that function may vary when called
59/// multiple times in the same program. `getPathToExecutable` will not succeed
60/// unless it returns a valid path to the executable file.
61///
62/// ## Usage {#bdls_processutil-usage}
63///
64///
65/// This section illustrates intended use of this component.
66///
67/// ### Example 1: Basic Usage {#bdls_processutil-example-1-basic-usage}
68///
69///
70/// Get the current process ID:
71/// @code
72/// const int pid = bdls::ProcessUtil::getProcessId();
73/// @endcode
74/// All calls to `getProcessId` will yield the same value:
75/// @code
76/// assert(bdls::ProcessUtil::getProcessId() == pid);
77/// @endcode
78/// Get the current process name:
79/// @code
80/// bsl::string processName;
81/// int rc = bdls::ProcessUtil::getProcessName(&processName);
82/// if (0 != rc) {
83/// processName = "unknown";
84/// }
85/// @endcode
86/// All calls to `getProcessName` will yield the same value. Note that if
87/// the call does not succeed, `processNameB` will not be modified.
88/// @code
89/// bsl::string processNameB("unknown");
90/// (void) bdls::ProcessUtil::getProcessName(&processNameB);
91///
92/// assert(processNameB == processName);
93/// @endcode
94/// @}
95/** @} */
96/** @} */
97
98/** @addtogroup bdl
99 * @{
100 */
101/** @addtogroup bdls
102 * @{
103 */
104/** @addtogroup bdls_processutil
105 * @{
106 */
107
108#include <bdlscm_version.h>
109
110#include <bsl_string.h>
111
112
113namespace bdls {
114
115 // ==================
116 // struct ProcessUtil
117 // ==================
118
119/// This `struct` contains utility methods for platform-independent process
120/// operations.
122
123 // CLASS METHODS
124
125 /// Return the system specific process identifier for the currently running
126 /// process.
127 static int getProcessId();
128
129 /// Load the system specific process name for the currently running process
130 /// into the specified `*result`. Return 0 on success, and a non-zero
131 /// value otherwise. The language in which `*result` is provided is
132 /// unspecified; `*result` will be encoded as UTF-8, but might not be
133 /// normalized. On failure, `*result` will be unmodified. Note that the
134 /// primary purpose of this method is to provide an identifier for the
135 /// current process, and `*result` may not be a valid path to the
136 /// executable; to access the actual task file for the process use
137 /// `getPathToExecutable` below.
138 static int getProcessName(bsl::string *result);
139
140 /// Set `*result` to a path with which the executable can be accessed
141 /// (which may bear no relation to the command line used to begin this
142 /// process). Return 0 on success, and a non-zero value otherwise. On
143 /// failure, `*result` will not be modified. Note that the returned value
144 /// of `*result` may not correspond to the value of `argv[0]` passed to
145 /// `main`. Some systems provide more reliable alternatives, such as
146 /// through the "/proc" file system.
147 static int getPathToExecutable(bsl::string *result);
148};
149
150} // close package namespace
151
152
153#endif
154
155// ----------------------------------------------------------------------------
156// Copyright 2015 Bloomberg Finance L.P.
157//
158// Licensed under the Apache License, Version 2.0 (the "License");
159// you may not use this file except in compliance with the License.
160// You may obtain a copy of the License at
161//
162// http://www.apache.org/licenses/LICENSE-2.0
163//
164// Unless required by applicable law or agreed to in writing, software
165// distributed under the License is distributed on an "AS IS" BASIS,
166// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
167// See the License for the specific language governing permissions and
168// limitations under the License.
169// ----------------------------- END-OF-FILE ----------------------------------
170
171/** @} */
172/** @} */
173/** @} */
Definition bslstl_string.h:1281
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdls_fdstreambuf.h:412
Definition bdls_processutil.h:121
static int getPathToExecutable(bsl::string *result)
static int getProcessName(bsl::string *result)
static int getProcessId()