Collect more platform-specific code to sysinfo.h
authorTeemu Murtola <teemu.murtola@gmail.com>
Sun, 19 Oct 2014 04:24:36 +0000 (07:24 +0300)
committerTeemu Murtola <teemu.murtola@gmail.com>
Wed, 22 Oct 2014 18:44:56 +0000 (21:44 +0300)
Move a few existing functions of this type to sysinfo.* from
basenetwork.* and cstringutil.*.  Mostly this is just code movement, but
there are two changes:
 * getuid() usage is now controlled by HAVE_UNISTD_H instead of
   HAVE_PWD_H, and excluded for MinGW (the latter is just a guess, but
   many other similar calls are excluded for MinGW).
 * Getting the current username uses getlogin_r() instead of getpwuid(),
   following the implementation in tngio.cpp instead of the original
   implementation in cstringutil.c.  If this is OK, then we can get rid
   of HAVE_PWD_H completely.  Alternative would be to make the code use
   getpwuid_r() if the old functionality is more desirable, but that is
   a bit more effort, and requires some testing for platform support, as
   that has not been used before in the code.

This allows removing platform-specific #ifdefs and #includes from
multiple files completely, making these aspects better encapsulated.

While checking the presence of #ifdefs, removed one useless #ifndef
GMX_FAHCORE from tngio.cpp; make_backup() already handles FAHCORE
internally.

Change-Id: I46876635fb254eb9558cce80b34ebe3078425375

13 files changed:
src/gromacs/fileio/tngio.cpp
src/gromacs/fileio/xvgr.cpp
src/gromacs/gmxana/gmx_covar.c
src/gromacs/gmxlib/checkpoint.cpp
src/gromacs/gmxlib/gmx_detect_hardware.cpp
src/gromacs/gmxlib/main.cpp
src/gromacs/mdlib/sim_util.cpp
src/gromacs/utility/basenetwork.cpp
src/gromacs/utility/basenetwork.h
src/gromacs/utility/cstringutil.c
src/gromacs/utility/cstringutil.h
src/gromacs/utility/sysinfo.cpp
src/gromacs/utility/sysinfo.h

index 2728b0839e48b612928c517b1938c51bd89dcade..2ae31c288ec8027b20e44edc3a818da2db567c34 100644 (file)
 
 #include "config.h"
 
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
 #ifdef GMX_USE_TNG
 #include "tng/tng_io.h"
 #endif
 #include "gromacs/math/utilities.h"
 #include "gromacs/topology/topology.h"
 #include "gromacs/utility/basedefinitions.h"
-#include "gromacs/utility/basenetwork.h"
 #include "gromacs/utility/fatalerror.h"
 #include "gromacs/utility/gmxassert.h"
 #include "gromacs/utility/programcontext.h"
+#include "gromacs/utility/sysinfo.h"
 
 static const char *modeToVerb(char mode)
 {
@@ -90,10 +86,7 @@ void gmx_tng_open(const char       *filename,
      */
     if (mode == 'w')
     {
-#ifndef GMX_FAHCORE
-        /* only make backups for normal gromacs */
         make_backup(filename);
-#endif
     }
 
     /* tng must not be pointing at already allocated memory.
@@ -149,9 +142,8 @@ void gmx_tng_open(const char       *filename,
 //             tng_last_program_name_set(*tng, programInfo);
 //         }
 
-#if defined(HAVE_UNISTD_H) && !defined(__MINGW32__)
         char username[256];
-        if (!getlogin_r(username, 256))
+        if (!gmx_getusername(username, 256))
         {
             if (mode == 'w')
             {
@@ -164,7 +156,6 @@ void gmx_tng_open(const char       *filename,
 //         {
 //             tng_last_user_name_set(*tng, username);
 //         }
-#endif
     }
 #else
     gmx_file("GROMACS was compiled without TNG support, cannot handle this file type");
index a9cb9b8f3240a1790e61a7a67fcf427887397d5b..f38f950b8c4bbf977d08ce59f0a6f825c2a1414c 100644 (file)
 
 #include "xvgr.h"
 
-#include "config.h"
-
 #include <ctype.h>
 #include <string.h>
-#include <time.h>
-
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
 
 #include "gromacs/fileio/gmxfio.h"
 #include "gromacs/legacyheaders/copyrite.h"
@@ -57,6 +50,7 @@
 #include "gromacs/utility/fatalerror.h"
 #include "gromacs/utility/futil.h"
 #include "gromacs/utility/smalloc.h"
+#include "gromacs/utility/sysinfo.h"
 
 gmx_bool output_env_get_print_xvgr_codes(const output_env_t oenv)
 {
@@ -238,12 +232,10 @@ void xvgr_header(FILE *fp, const char *title, const char *xaxis,
                  const output_env_t oenv)
 {
     char   pukestr[100], buf[STRLEN];
-    time_t t;
 
     if (output_env_get_print_xvgr_codes(oenv))
     {
-        time(&t);
-        gmx_ctime_r(&t, buf, STRLEN);
+        gmx_format_current_time(buf, STRLEN);
         fprintf(fp, "# This file was created %s", buf);
         try
         {
index 57756726ed606c51d904c15df3099d8a758926c1..6154b3c3c5cf429e35bda32b64f73cf4a176acbb 100644 (file)
  */
 #include "gmxpre.h"
 
-#include "config.h"
-
 #include <math.h>
 #include <string.h>
-#include <time.h>
-
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
 
 #include "gromacs/commandline/pargs.h"
 #include "gromacs/fileio/confio.h"
@@ -66,6 +59,7 @@
 #include "gromacs/utility/fatalerror.h"
 #include "gromacs/utility/futil.h"
 #include "gromacs/utility/smalloc.h"
+#include "gromacs/utility/sysinfo.h"
 
 int gmx_covar(int argc, char *argv[])
 {
@@ -139,7 +133,6 @@ int gmx_covar(int argc, char *argv[])
     int             d, dj, nfit;
     atom_id        *index, *ifit;
     gmx_bool        bDiffMass1, bDiffMass2;
-    time_t          now;
     char            timebuf[STRLEN];
     t_rgb           rlo, rmi, rhi;
     real           *eigenvectors;
@@ -600,8 +593,7 @@ int gmx_covar(int argc, char *argv[])
 
     out = gmx_ffopen(logfile, "w");
 
-    time(&now);
-    gmx_ctime_r(&now, timebuf, STRLEN);
+    gmx_format_current_time(timebuf, STRLEN);
     fprintf(out, "Covariance analysis log, written %s\n", timebuf);
 
     fprintf(out, "Program: %s\n", argv[0]);
index c1a806c42dc785c6bb80ba0fbc0c0b865bbe9b5c..a9e1ae0f7013a5bcce8e4b5626b5e66cbbaa4842 100644 (file)
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
-#include <time.h>
 
 #include <fcntl.h>
 
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 #include "gromacs/legacyheaders/typedefs.h"
 #include "gromacs/legacyheaders/types/commrec.h"
 #include "gromacs/math/vec.h"
-#include "gromacs/utility/basenetwork.h"
 #include "gromacs/utility/baseversion.h"
 #include "gromacs/utility/cstringutil.h"
 #include "gromacs/utility/fatalerror.h"
 #include "gromacs/utility/futil.h"
 #include "gromacs/utility/smalloc.h"
+#include "gromacs/utility/sysinfo.h"
 
 #ifdef GMX_FAHCORE
 #include "corewrap.h"
@@ -1502,7 +1498,6 @@ void write_checkpoint(const char *fn, gmx_bool bNumberAndKeep,
     int                  double_prec;
     char                *fprog;
     char                *fntemp; /* the temporary checkpoint file name */
-    time_t               now;
     char                 timebuf[STRLEN];
     int                  nppnodes, npmenodes;
     char                 buf[1024], suffix[5+STEPSTRSIZE], sbuf[STEPSTRSIZE];
@@ -1538,8 +1533,7 @@ void write_checkpoint(const char *fn, gmx_bool bNumberAndKeep,
     snew(fntemp, strlen(fn));
     strcpy(fntemp, fn);
 #endif
-    time(&now);
-    gmx_ctime_r(&now, timebuf, STRLEN);
+    gmx_format_current_time(timebuf, STRLEN);
 
     if (fplog)
     {
index bc7817d5f614b2d16b704f1efa321a4c76097f67..f1e18bb3d01744b74e9d2b0defe4199273f1f163 100644 (file)
@@ -71,6 +71,7 @@
 #include "gromacs/utility/gmxomp.h"
 #include "gromacs/utility/smalloc.h"
 #include "gromacs/utility/stringutil.h"
+#include "gromacs/utility/sysinfo.h"
 
 #ifdef GMX_GPU
 const gmx_bool bGPUBinary = TRUE;
index deea126674bb394a24a63eb457d5f125ac782925..733658091506178b0f2c5a9c23b6252764ca5655 100644 (file)
@@ -44,7 +44,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <time.h>
 
 #include "gromacs/fileio/filenm.h"
 #include "gromacs/fileio/gmxfio.h"
@@ -52,7 +51,6 @@
 #include "gromacs/legacyheaders/macros.h"
 #include "gromacs/legacyheaders/network.h"
 #include "gromacs/legacyheaders/types/commrec.h"
-#include "gromacs/utility/basenetwork.h"
 #include "gromacs/utility/cstringutil.h"
 #include "gromacs/utility/exceptions.h"
 #include "gromacs/utility/fatalerror.h"
@@ -216,7 +214,6 @@ void gmx_log_open(const char *lognm, const t_commrec *cr,
 {
     int    pid;
     char   host[256];
-    time_t t;
     char   timebuf[STRLEN];
     FILE  *fp = *fplog;
 
@@ -231,10 +228,8 @@ void gmx_log_open(const char *lognm, const t_commrec *cr,
 
     /* Get some machine parameters */
     gmx_gethostname(host, 256);
-
-    time(&t);
-
     pid = gmx_getpid();
+    gmx_format_current_time(timebuf, STRLEN);
 
     if (bAppendFiles)
     {
@@ -247,8 +242,6 @@ void gmx_log_open(const char *lognm, const t_commrec *cr,
                 );
     }
 
-    gmx_ctime_r(&t, timebuf, STRLEN);
-
     fprintf(fp,
             "Log file opened on %s"
             "Host: %s  pid: %d  rank ID: %d  number of ranks:  %d\n",
index 723223d7d121e91af9d5918684930c32de6a2ed4..e35e31a8e60044796c1c9544952f14757386949c 100644 (file)
 #include <stdio.h>
 #include <string.h>
 
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-
 #include "gromacs/essentialdynamics/edsam.h"
 #include "gromacs/ewald/pme.h"
 #include "gromacs/gmxlib/nonbonded/nb_free_energy.h"
@@ -97,6 +93,7 @@
 #include "gromacs/utility/cstringutil.h"
 #include "gromacs/utility/gmxmpi.h"
 #include "gromacs/utility/smalloc.h"
+#include "gromacs/utility/sysinfo.h"
 
 #include "adress.h"
 
index c3a90db5d1085622167ce936e73ee7b78413de48..ce1197c8cad4ba83d21927dd9117785692d0e0b2 100644 (file)
 
 #include <exception>
 
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
 #include "gromacs/utility/cstringutil.h"
 #include "gromacs/utility/fatalerror.h"
 #include "gromacs/utility/gmxmpi.h"
 #include "gromacs/utility/programcontext.h"
 
-int gmx_gethostname(char *name, size_t len)
-{
-    if (len < 8)
-    {
-        gmx_incons("gmx_gethostname called with len<8");
-    }
-#if defined(HAVE_UNISTD_H) && !defined(__native_client__) && !defined(__MINGW32__)
-    if (gethostname(name, len-1) != 0)
-    {
-        std::strncpy(name, "unknown", 8);
-        return -1;
-    }
-    return 0;
-#else
-    std::strncpy(name, "unknown", 8);
-    return -1;
-#endif
-}
-
 gmx_bool gmx_mpi_initialized(void)
 {
 #ifndef GMX_MPI
index d93714c992b87fd3cd97e1a591398b7af796d62e..a03d6f698f729013764d04ac83a2de4cc3293cb3 100644 (file)
 extern "C" {
 #endif
 
-/*! \brief
- * Sets the hostname to the value given by gethostname(), if available.
- *
- * \param[out] name Buffer to receive the hostname.
- * \param[in]  len  Length of buffer \p name (must be >= 8).
- * \returns 0 on success, -1 on error.
- *
- * If the value is not available, "unknown" is returned.
- * \p name should have at least size \p len.
- */
-int gmx_gethostname(char *name, size_t len);
-
 /*! \brief
  * Returns whether MPI has been initialized.
  *
index 14f010c077cc26bf34b63c065e43ff22c560ecc1..438a232b9c39b248b35e46d918ee6c8afec09fc7 100644 (file)
 
 #include "cstringutil.h"
 
-#include "config.h"
-
 #include <assert.h>
 #include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <time.h>
-
-#include <sys/types.h>
-
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-#ifdef HAVE_PWD_H
-#include <pwd.h>
-#endif
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
 
 #include "gromacs/utility/basedefinitions.h"
-#include "gromacs/utility/basenetwork.h"
 #include "gromacs/utility/fatalerror.h"
 #include "gromacs/utility/futil.h"
 #include "gromacs/utility/smalloc.h"
+#include "gromacs/utility/sysinfo.h"
 
 int continuing(char *s)
 {
@@ -190,72 +175,28 @@ void trim (char *str)
     rtrim (str);
 }
 
-char *
-gmx_ctime_r(const time_t *clock, char *buf, int n)
+void nice_header(FILE *out, const char *fn)
 {
-#ifdef _MSC_VER
-    /* Windows */
-    ctime_s( buf, n, clock );
-#elif defined(GMX_NATIVE_WINDOWS)
-    char *tmpbuf = ctime( clock );
-    strncpy(buf, tmpbuf, n-1);
-    buf[n-1] = '\0';
-#elif (defined(__sun))
-    /*Solaris*/
-    ctime_r(clock, buf, n);
-#else
-    char tmpbuf[STRLEN];
-    ctime_r(clock, tmpbuf);
-    strncpy(buf, tmpbuf, n-1);
-    buf[n-1] = '\0';
-#endif
-    return buf;
-}
-
-void nice_header (FILE *out, const char *fn)
-{
-    const char    *unk = "onbekend";
-    time_t         clock;
-    const char    *user;
-    int            gh;
-#ifdef HAVE_PWD_H
-    uid_t          uid;
-#else
     int            uid;
-#endif
-    char           buf[256] = "";
+    char           userbuf[256];
+    char           hostbuf[256];
     char           timebuf[STRLEN];
-#ifdef HAVE_PWD_H
-    struct passwd *pw;
-#endif
 
     /* Print a nice header above the file */
-    time(&clock);
-    fprintf (out, "%c\n", COMMENTSIGN);
-    fprintf (out, "%c\tFile '%s' was generated\n", COMMENTSIGN, fn ? fn : unk);
-
-#ifdef HAVE_PWD_H
-    uid  = getuid();
-    pw   = getpwuid(uid);
-    gh   = gmx_gethostname(buf, 255);
-    /* pw returns null on error (e.g. compute nodes lack /etc/passwd) */
-    user = pw ? pw->pw_name : unk;
-#else
-    uid  = 0;
-    gh   = -1;
-    user = unk;
-#endif
-
-    gmx_ctime_r(&clock, timebuf, STRLEN);
-    fprintf (out, "%c\tBy user: %s (%d)\n", COMMENTSIGN,
-             user ? user : unk, (int) uid);
-    fprintf(out, "%c\tOn host: %s\n", COMMENTSIGN, (gh == 0) ? buf : unk);
-
-    fprintf (out, "%c\tAt date: %s", COMMENTSIGN, timebuf);
-    fprintf (out, "%c\n", COMMENTSIGN);
+    fprintf(out, "%c\n", COMMENTSIGN);
+    fprintf(out, "%c\tFile '%s' was generated\n", COMMENTSIGN, fn ? fn : "unknown");
+
+    uid  = gmx_getuid();
+    gmx_getusername(userbuf, 256);
+    gmx_gethostname(hostbuf, 256);
+    gmx_format_current_time(timebuf, STRLEN);
+
+    fprintf(out, "%c\tBy user: %s (%d)\n", COMMENTSIGN, userbuf, uid);
+    fprintf(out, "%c\tOn host: %s\n", COMMENTSIGN, hostbuf);
+    fprintf(out, "%c\tAt date: %s\n", COMMENTSIGN, timebuf);
+    fprintf(out, "%c\n", COMMENTSIGN);
 }
 
-
 int gmx_strcasecmp_min(const char *str1, const char *str2)
 {
     char ch1, ch2;
index 9bd8e3b85e8d0ededf394c8cffbdb95793db4b59..41e7e9cb329f672d4e44aaa023cf7588a8a28bee 100644 (file)
@@ -46,7 +46,6 @@
 #include "gromacs/utility/gmx_header_config.h"
 
 #include <stdio.h>
-#include <time.h>
 
 #include "gromacs/utility/basedefinitions.h"
 
@@ -95,9 +94,6 @@ void rtrim(char *str);
 /** Remove leading and trailing whitespace from a string. */
 void trim(char *str);
 
-/** Portable version of ctime_r. */
-char *gmx_ctime_r(const time_t *clock, char *buf, int n);
-
 /** Prints creation time stamp and user information into a file as comments. */
 void nice_header(FILE *out, const char *fn);
 
index 64f9ff29bf43d2958d70427b269907f58e73df7d..d1a5353f1104445b9cde6c2a64fb905cfa514692 100644 (file)
 
 #include "config.h"
 
+#include <string.h>
+#include <time.h>
+
+#include <sys/types.h>
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
 #ifdef GMX_NATIVE_WINDOWS
 #include <process.h>
 #endif
+#ifdef HAVE_PWD_H
+#include <pwd.h>
+#endif
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 
+#include "gromacs/utility/gmxassert.h"
+
+namespace
+{
+//! Static return value for cases when a string value is not available.
+const char c_unknown[] = "unknown";
+} // namespace
+
+int gmx_gethostname(char *buf, size_t len)
+{
+    GMX_RELEASE_ASSERT(len >= 8, "Input buffer is too short");
+#if defined(HAVE_UNISTD_H) && !defined(__native_client__) && !defined(__MINGW32__)
+    if (gethostname(buf, len-1) == 0)
+    {
+        buf[len-1] = '\0';
+        return 0;
+    }
+#endif
+    strcpy(buf, c_unknown);
+    return -1;
+}
+
 int gmx_getpid()
 {
 #ifdef GMX_NATIVE_WINDOWS
@@ -60,3 +92,56 @@ int gmx_getpid()
     return getpid();
 #endif
 }
+
+int gmx_getuid()
+{
+#if defined(HAVE_UNISTD_H) && !defined(__MINGW32__)
+    return getuid();
+#else
+    return -1;
+#endif
+}
+
+int gmx_getusername(char *buf, size_t len)
+{
+    GMX_RELEASE_ASSERT(len >= 8, "Input buffer is too short");
+    // TODO: nice_header() used getpwuid() instead; consider using getpwuid_r()
+    // here.  If not, get rid of HAVE_PWD_H completely.
+#if defined(HAVE_UNISTD_H) && !defined(__MINGW32__)
+    if (!getlogin_r(buf, len))
+    {
+        buf[len-1] = '\0';
+        return 0;
+    }
+#endif
+    strcpy(buf, c_unknown);
+    return -1;
+}
+
+char *
+gmx_ctime_r(const time_t *clock, char *buf, size_t len)
+{
+#ifdef _MSC_VER
+    /* Windows */
+    ctime_s(buf, len, clock);
+#elif defined(GMX_NATIVE_WINDOWS)
+    char *tmpbuf = ctime(clock);
+    strncpy(buf, tmpbuf, len-1);
+    buf[len-1] = '\0';
+#elif (defined(__sun))
+    /*Solaris*/
+    ctime_r(clock, buf, len);
+#else
+    char tmpbuf[30];
+    ctime_r(clock, tmpbuf);
+    strncpy(buf, tmpbuf, len-1);
+    buf[len-1] = '\0';
+#endif
+    return buf;
+}
+
+void gmx_format_current_time(char *buf, size_t len)
+{
+    time_t clock = time(NULL);
+    gmx_ctime_r(&clock, buf, len);
+}
index 919c7b623fd4f924d9d859794c02137094801d1f..659048d4b14304e3c1325d213cfb0adfa5d98da0 100644 (file)
 #ifndef GMX_UTILITY_SYSINFO_H
 #define GMX_UTILITY_SYSINFO_H
 
+#include <stddef.h>
+#include <time.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+/*! \addtogroup module_utility
+ * \{
+ */
+
+/*! \brief
+ * Gets the hostname as given by gethostname(), if available.
+ *
+ * \param[out] buf  Buffer to receive the hostname.
+ * \param[in]  len  Length of buffer \p buf (must be >= 8).
+ * \returns 0 on success, -1 on error.
+ *
+ * If the value is not available, "unknown" is returned.
+ * \p name should have at least size \p len.
+ *
+ * Does not throw.
+ */
+int gmx_gethostname(char *buf, size_t len);
+
 /*! \brief
  * Returns the process ID of the current process.
  *
  * Does not throw.
+ */
+int gmx_getpid(void);
+/*! \brief
+ * Returns the current user ID, or -1 if not available.
  *
- * \ingroup module_utility
+ * Does not throw.
+ */
+int gmx_getuid(void);
+/*! \brief
+ * Gets the current user name, if available.
+ *
+ * \param[out] buf  Buffer to receive the username.
+ * \param[in]  len  Length of buffer \p buf (must be >= 8).
+ * \returns 0 on success, -1 on error.
+ *
+ * Does not throw.
  */
-int gmx_getpid();
+int gmx_getusername(char *buf, size_t len);
+
+/*! \brief
+ * Portable version of ctime_r.
+ *
+ * Does not throw.
+ */
+char *gmx_ctime_r(const time_t *clock, char *buf, size_t len);
+/*! \brief
+ * Gets the current time as a string.
+ *
+ * \param[out] buf  Buffer to receive the string.
+ * \param[in]  len  Length of buffer \p buf (26 characters should be sufficient).
+ *
+ * Does not throw.
+ */
+void gmx_format_current_time(char *buf, size_t len);
+
+/*! \} */
 
 #ifdef __cplusplus
 }