The function is needed by both cron and anacron.
Signed-off-by: Sami Kerola <kerolasa(a)iki.fi>
---
anacron/runjob.c | 10 ++++++----
cronie_common.h | 29 +++++++++++++++++++++++++++++
src/database.c | 1 +
src/do_command.c | 1 +
src/funcs.h | 6 ++----
src/misc.c | 27 ---------------------------
6 files changed, 39 insertions(+), 35 deletions(-)
diff --git a/anacron/runjob.c b/anacron/runjob.c
index 9656254..b2dd93b 100644
--- a/anacron/runjob.c
+++ b/anacron/runjob.c
@@ -36,6 +36,7 @@
#include "global.h"
#include <langinfo.h>
+#include "cronie_common.h"
static int
temp_file(job_rec *jr)
@@ -236,13 +237,13 @@ launch_job(job_rec *jr)
{
pid_t pid;
int fd;
- char hostname[512];
+ char *hostname;
char *mailto;
/* get hostname */
- if (gethostname(hostname, 512)) {
- strcpy (hostname,"unknown machine");
- }
+ do
+ hostname = xgethostname();
+ while (hostname == NULL && usleep(1000));
setup_env(jr);
@@ -276,6 +277,7 @@ launch_job(job_rec *jr)
xwrite(fd, "' on ");
xwrite(fd, hostname);
xwrite(fd, "\n\n");
+ free(hostname);
jr->mail_header_size = file_size(fd);
diff --git a/cronie_common.h b/cronie_common.h
index 4ce172e..13c8be7 100644
--- a/cronie_common.h
+++ b/cronie_common.h
@@ -20,6 +20,8 @@
#ifndef CRONIE_COMMON_H
#define CRONIE_COMMON_H
+#include <stdlib.h>
+
#ifndef __attribute__
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
# define __attribute__(x) /* empty */
@@ -34,4 +36,31 @@
# define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
#endif
+static inline size_t get_hostname_max(void) {
+ long len = sysconf(_SC_HOST_NAME_MAX);
+
+ if (0 < len)
+ return len;
+#ifdef MAXHOSTNAMELEN
+ return MAXHOSTNAMELEN;
+#elif HOST_NAME_MAX
+ return HOST_NAME_MAX;
+#endif
+ return 64;
+}
+
+static inline char *xgethostname(void) {
+ char *name;
+ size_t sz = get_hostname_max() + 1;
+
+ name = malloc(sizeof (char) * sz);
+ if (!name)
+ return NULL;
+ if (gethostname(name, sz) != 0)
+ return NULL;
+
+ name[sz - 1] = '\0';
+ return name;
+}
+
#endif /* CRONIE_COMMON_H */
diff --git a/src/database.c b/src/database.c
index 50bfd1b..3aa3f15 100644
--- a/src/database.c
+++ b/src/database.c
@@ -43,6 +43,7 @@
# include <sys/inotify.h>
#endif
+#include "cronie_common.h"
#include "funcs.h"
#include "globals.h"
#include "pathnames.h"
diff --git a/src/do_command.c b/src/do_command.c
index f4ed647..12ccc23 100644
--- a/src/do_command.c
+++ b/src/do_command.c
@@ -31,6 +31,7 @@
#include <sys/wait.h>
#include <unistd.h>
+#include "cronie_common.h"
#include "externs.h"
#include "funcs.h"
#include "globals.h"
diff --git a/src/funcs.h b/src/funcs.h
index d9afd48..4d95d22 100644
--- a/src/funcs.h
+++ b/src/funcs.h
@@ -73,8 +73,7 @@ int load_database(cron_db *),
allowed(const char * ,const char * ,const char *),
strdtb(char *);
-size_t strlens(const char *, ...),
- get_hostname_max(void);
+size_t strlens(const char *, ...);
char *env_get(const char *, char **),
*arpadate(time_t *),
@@ -82,8 +81,7 @@ char *env_get(const char *, char **),
*first_word(const char *, const char *),
**env_init(void),
**env_copy(char **),
- **env_set(char **, char *),
- *xgethostname(void);
+ **env_set(char **, char *);
user *load_user(int, struct passwd *, const char *, const char *, const char *),
*find_user(cron_db *, const char *, const char *);
diff --git a/src/misc.c b/src/misc.c
index e696171..b0f97be 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -739,33 +739,6 @@ size_t strlens(const char *last, ...) {
return (ret);
}
-size_t get_hostname_max(void) {
- long len = sysconf(_SC_HOST_NAME_MAX);
-
- if (0 < len)
- return len;
-#ifdef MAXHOSTNAMELEN
- return MAXHOSTNAMELEN;
-#elif HOST_NAME_MAX
- return HOST_NAME_MAX;
-#endif
- return 64;
-}
-
-char *xgethostname(void) {
- char *name;
- size_t sz = get_hostname_max() + 1;
-
- name = malloc(sizeof (char) * sz);
- if (!name)
- return NULL;
- if (gethostname(name, sz) != 0)
- return NULL;
-
- name[sz - 1] = '\0';
- return name;
-}
-
/* Return the offset from GMT in seconds (algorithm taken from sendmail).
*
* warning:
--
1.8.0.1