Workaround for MemorySanitizer
[alexxy/gromacs.git] / src / gromacs / utility / sysinfo.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements functions from sysinfo.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_utility
41  */
42 #include "gmxpre.h"
43
44 #include "sysinfo.h"
45
46 #include "config.h"
47
48 #include <string.h>
49 #include <time.h>
50
51 #include <sys/types.h>
52 #ifdef HAVE_SYS_TIME_H
53 #include <sys/time.h>
54 #endif
55 #ifdef GMX_NATIVE_WINDOWS
56 #include <Windows.h>
57 #include <process.h>
58 #endif
59 #ifdef HAVE_PWD_H
60 #include <pwd.h>
61 #endif
62 #ifdef HAVE_UNISTD_H
63 #include <unistd.h>
64 #endif
65
66 #include "gromacs/utility/basedefinitions.h"
67 #include "gromacs/utility/gmxassert.h"
68
69 namespace
70 {
71 //! Static return value for cases when a string value is not available.
72 const char c_unknown[] = "unknown";
73 } // namespace
74
75 int gmx_gethostname(char *buf, size_t len)
76 {
77     GMX_RELEASE_ASSERT(len >= 8, "Input buffer is too short");
78 #ifdef GMX_NATIVE_WINDOWS
79     DWORD  dlen = len;
80     if (GetComputerName(buf, &dlen))
81     {
82         return 0;
83     }
84 #elif defined(HAVE_UNISTD_H) && !defined(__native_client__)
85     if (gethostname(buf, len-1) == 0)
86     {
87         buf[len-1] = '\0';
88         return 0;
89     }
90 #endif
91     strcpy(buf, c_unknown);
92     return -1;
93 }
94
95 int gmx_getpid()
96 {
97 #ifdef GMX_NATIVE_WINDOWS
98     return _getpid();
99 #else
100     return getpid();
101 #endif
102 }
103
104 int gmx_getuid()
105 {
106 #if defined(HAVE_UNISTD_H) && !defined(__MINGW32__)
107     return getuid();
108 #else
109     return -1;
110 #endif
111 }
112
113 int gmx_getusername(char *buf, size_t len)
114 {
115     GMX_RELEASE_ASSERT(len >= 8, "Input buffer is too short");
116     // TODO: nice_header() used getpwuid() instead; consider using getpwuid_r()
117     // here.  If not, get rid of HAVE_PWD_H completely.
118 #ifdef GMX_NATIVE_WINDOWS
119     DWORD  dlen = len;
120     if (GetUserName(buf, &dlen))
121     {
122         return 0;
123     }
124 #elif defined(HAVE_UNISTD_H) && !__has_feature(memory_sanitizer) //MSAN Issue 83
125     if (!getlogin_r(buf, len))
126     {
127         buf[len-1] = '\0';
128         return 0;
129     }
130 #endif
131     strcpy(buf, c_unknown);
132     return -1;
133 }
134
135 char *
136 gmx_ctime_r(const time_t *clock, char *buf, size_t len)
137 {
138 #ifdef _MSC_VER
139     /* Windows */
140     ctime_s(buf, len, clock);
141 #elif defined(GMX_NATIVE_WINDOWS)
142     char *tmpbuf = ctime(clock);
143     strncpy(buf, tmpbuf, len-1);
144     buf[len-1] = '\0';
145 #elif (defined(__sun))
146     /*Solaris*/
147     ctime_r(clock, buf, len);
148 #else
149     char tmpbuf[30];
150     ctime_r(clock, tmpbuf);
151     strncpy(buf, tmpbuf, len-1);
152     buf[len-1] = '\0';
153 #endif
154     return buf;
155 }
156
157 void gmx_format_current_time(char *buf, size_t len)
158 {
159     time_t clock = time(NULL);
160     gmx_ctime_r(&clock, buf, len);
161 }
162
163 int gmx_set_nice(int level)
164 {
165 #ifdef GMX_USE_NICE
166     // TODO: This may not be reliable, but currently the return value is not
167     // used.
168     if (nice(level) != -1)
169     {
170         return 0;
171     }
172 #else
173     GMX_UNUSED_VALUE(level);
174 #endif
175     return -1;
176 }