]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
dovecot -n/-a: Print some information about the system.
authorTimo Sirainen <tss@iki.fi>
Wed, 29 Oct 2008 18:16:40 +0000 (20:16 +0200)
committerTimo Sirainen <tss@iki.fi>
Wed, 29 Oct 2008 18:16:40 +0000 (20:16 +0200)
--HG--
branch : HEAD

configure.in
src/master/Makefile.am
src/master/main.c
src/master/sysinfo-get.c [new file with mode: 0644]
src/master/sysinfo-get.h [new file with mode: 0644]

index 545ed26e73b3e125497bf549c45d16da80c4fb9d..77c83f56d482dc75cfc62164e8d45ed5448876cc 100644 (file)
@@ -20,7 +20,7 @@ AC_CHECK_HEADERS(strings.h stdint.h unistd.h dirent.h malloc.h inttypes.h \
   sys/quota.h sys/fs/ufs_quota.h ufs/ufs/quota.h jfs/quota.h sys/fs/quota_common.h \
   mntent.h sys/mnttab.h sys/event.h sys/time.h sys/mkdev.h linux/dqblk_xfs.h \
   xfs/xqm.h sasl.h sasl/sasl.h execinfo.h ucontext.h malloc_np.h sys/utsname.h \
-  sys/vmount.h)
+  sys/vmount.h sys/utsname.h)
 
 AC_ARG_ENABLE(ipv6,
 [  --enable-ipv6           Enable IPv6 support (auto)],
index 8f918e51c5dc9c20caab6b61070b864df690a2de..aae3d7ebc06eded726d6c53fcb690514cf7695c7 100644 (file)
@@ -32,7 +32,8 @@ dovecot_SOURCES = \
        main.c \
        master-settings.c \
        syslog-util.c \
-       ssl-init.c
+       ssl-init.c \
+       sysinfo-get.c
 
 noinst_HEADERS = \
        auth-process.h \
@@ -49,7 +50,8 @@ noinst_HEADERS = \
        master-login-interface.h \
        master-settings.h \
        syslog-util.h \
-       ssl-init.h
+       ssl-init.h \
+       sysinfo-get.h
 
 EXTRA_DIST = \
        master-settings-defs.c
index 74313d78daefa1f0fcf2eea05191c217c3c5ba9e..bcb4e1e51007b866cd1d0c40a06c002c4a7abfce 100644 (file)
@@ -20,6 +20,7 @@
 #include "listener.h"
 #include "ssl-init.h"
 #include "log.h"
+#include "sysinfo-get.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -527,6 +528,7 @@ int main(int argc, char *argv[])
        }
 
        if (dump_config) {
+
                /* print the config file path before parsing it, so in case
                   of errors it's still shown */
                printf("# "VERSION": %s\n", configfile);
@@ -541,6 +543,12 @@ int main(int argc, char *argv[])
        } T_END;
 
        if (dump_config) {
+               const char *info;
+
+               info = sysinfo_get(settings_root->defaults->mail_location);
+               if (*info != '\0')
+                       printf("# %s\n", info);
+
                master_settings_dump(settings_root, dump_config_nondefaults);
                return 0;
        }
diff --git a/src/master/sysinfo-get.c b/src/master/sysinfo-get.c
new file mode 100644 (file)
index 0000000..431774c
--- /dev/null
@@ -0,0 +1,118 @@
+/* Copyright (c) 2008 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "home-expand.h"
+#include "mountpoint.h"
+#include "strescape.h"
+#include "sysinfo-get.h"
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#ifdef HAVE_SYS_UTSNAME_H
+#  include <sys/utsname.h>
+#endif
+
+static bool readfile(const char *path, const char **data_r)
+{
+       char buf[1024];
+       int fd, ret;
+
+       fd = open(path, O_RDONLY);
+       if (fd == -1)
+               return FALSE;
+       ret = read(fd, buf, sizeof(buf));
+       (void)close(fd);
+       if (ret <= 0)
+               return FALSE;
+
+       *data_r = t_strndup(buf, ret);
+       return TRUE;
+}
+
+static bool lsb_distro_get(const char *path, const char **name_r)
+{
+       const char *data, *const *p, *str, *end;
+
+       if (!readfile(path, &data))
+               return FALSE;
+
+       for (p = t_strsplit(data, "\n"); *p != '\0'; p++) {
+               if (strncmp(*p, "DISTRIB_DESCRIPTION=", 20) == 0)
+                       break;
+       }
+       if (*p == '\0')
+               return FALSE;
+
+       str = t_strcut(*p + 20, '\n');
+       if (*str != '"')
+               *name_r = str;
+       else {
+               end = strrchr(++str, '"');
+               *name_r = str_unescape(p_strdup_until(unsafe_data_stack_pool,
+                                                     str, end));
+       }
+       return TRUE;
+}
+
+static const char *distro_get(void)
+{
+       static const char *files[] = {
+               "", "/etc/redhat-release",
+               "", "/etc/SuSE-release",
+               "", "/etc/mandriva-release",
+               "", "/etc/fedora-release",
+               "Debian ", "/etc/debian_version",
+               NULL
+       };
+       const char *name;
+       unsigned int i;
+
+       if (lsb_distro_get("/etc/lsb-release", &name))
+               return name;
+       for (i = 0; files[i] != NULL; i += 2) {
+               if (readfile(files[i+1], &name)) {
+                       return t_strconcat(files[i], t_strcut(name, '\n'),
+                                          NULL);
+               }
+       }
+       return "";
+}
+
+static const char *filesystem_get(const char *mail_location)
+{
+       struct mountpoint mp;
+       const char *path;
+
+       path = strchr(mail_location, ':');
+       if (path == NULL)
+               path = mail_location;
+       else
+               path = t_strcut(path + 1, ':');
+       path = home_expand(path);
+
+       if (mountpoint_get(path, pool_datastack_create(), &mp) < 0)
+               return "";
+       return mp.type == NULL ? "" : mp.type;
+}
+
+const char *sysinfo_get(const char *mail_location)
+{
+       const char *distro = "", *fs, *uname_info = "";
+#ifdef HAVE_SYS_UTSNAME_H
+       struct utsname u;
+
+       if (uname(&u) < 0)
+               i_error("uname() failed: %m");
+       else {
+               uname_info = t_strdup_printf("%s %s %s",
+                                            u.sysname, u.release, u.machine);
+       }
+       if (strcmp(u.sysname, "Linux") == 0)
+               distro = distro_get();
+#endif
+       fs = filesystem_get(mail_location);
+       if (*uname_info == '\0' && *distro == '\0' && *fs == '\0')
+               return "";
+       return t_strdup_printf("OS: %s %s %s %s %s", u.sysname, u.release, u.machine, distro, fs);
+}
diff --git a/src/master/sysinfo-get.h b/src/master/sysinfo-get.h
new file mode 100644 (file)
index 0000000..0f319cb
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef SYSINFO_GET_H
+#define SYSINFO_GET_H
+
+const char *sysinfo_get(const char *mail_location);
+
+#endif