Skip to content

Commit 8c819e2

Browse files
committed
opal: ensure opal_gethostname() always returns a value
We initially thought it was a safe bet that opal_gethostname() would never be called before opal_init(). However, it turns out that there are some cases -- e.g., developer debugging -- where it is useful to call opal_output() (which calls opal_gethostname()) before opal_init(). Hence, we need to guarantee that opal_gethostname() always returns a valid value. If opal_gethostname() finds NULL in opal_process_info.nodename, simply call the internal function to initialize opal_process_info.nodename. Signed-off-by: Jeff Squyres <[email protected]>
1 parent 1d0b87e commit 8c819e2

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

opal/runtime/opal.h

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,32 @@ OPAL_DECLSPEC void opal_warn_fork(void);
114114
*/
115115
OPAL_DECLSPEC int opal_register_params(void);
116116

117+
/**
118+
* Internal function. Should not be called directly (should only be
119+
* invoked internally by opal_init() and opal_gethostname()).
120+
*/
121+
OPAL_DECLSPEC int opal_init_gethostname(void);
122+
117123
/**
118124
* Wrapper to return the hostname value that is in
119-
opal_process_info.nodename, as opposed to calling gethostname()
120-
directly, which is not guaranteed to be null-terminated and
121-
varies in its behavior depending on implementation. The
122-
opal_process_info.nodename value is first populated in
123-
opal/runtime/opal_init.c
124-
*/
125-
static inline const char *opal_gethostname( void ) {
126-
assert( NULL != opal_process_info.nodename );
125+
* opal_process_info.nodename, as opposed to calling gethostname()
126+
* directly, which is not guaranteed to be null-terminated and varies
127+
* in its behavior depending on implementation. The
128+
* opal_process_info.nodename value is first populated in
129+
* opal/runtime/opal_init.c.
130+
*
131+
* NOTE: In some cases (usually: developer debugging), it is possible
132+
* that this function is invoked (e.g., via opal_output()) before
133+
* opal_init() has been invoked, and therefore
134+
* opal_process_info.nodename is still NULL. In those cases, just
135+
* call opal_init_gethostname() directly to fill in
136+
* opal_process_info.nodename.
137+
*/
138+
static inline const char *opal_gethostname(void)
139+
{
140+
if (NULL == opal_process_info.nodename) {
141+
opal_init_gethostname();
142+
}
127143
return opal_process_info.nodename;
128144
}
129145

opal/runtime/opal_init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ bool opal_warn_on_fork = true;
110110
* truncated. It also tries again in the case where gethostname returns an
111111
* error because the buffer is initially too short.
112112
*/
113-
static int opal_init_gethostname(void)
113+
int opal_init_gethostname(void)
114114
{
115115
size_t count, length = OPAL_LOCAL_MAXHOSTNAMELEN;
116116
int ret_val, num_tries = 0;

0 commit comments

Comments
 (0)