From f9538ed2d2cc8be07d35633b1f9e311c738d31c3 Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Tue, 28 Jul 2015 00:58:28 +0200 Subject: [PATCH] mx_util: Add mx_hostname() from mxq_util --- mx_util.c | 18 ++++++++++++++++++ mx_util.h | 1 + mxq_util.c | 19 ------------------- mxq_util.h | 2 -- mxqd.c | 4 ++-- mxqsub.c | 2 +- 6 files changed, 22 insertions(+), 24 deletions(-) diff --git a/mx_util.c b/mx_util.c index da611762..a9b55465 100644 --- a/mx_util.c +++ b/mx_util.c @@ -441,6 +441,24 @@ int mx_asprintf_forever(char **strp, const char *fmt, ...) return len; } +char *mx_hostname(void) +{ + static char hostname[1024] = ""; + int res; + + if (*hostname) + return hostname; + + res = gethostname(hostname, 1024); + if (res == -1) { + if (errno != ENAMETOOLONG) + assert_perror(errno); + hostname[1024-1] = 0; + } + + return hostname; +} + char *mx_dirname(char *path) { char *tmp; diff --git a/mx_util.h b/mx_util.h index 09cc545b..9a48b908 100644 --- a/mx_util.h +++ b/mx_util.h @@ -88,6 +88,7 @@ char *mx_strdup_forever(char *str); int mx_vasprintf_forever(char **strp, const char *fmt, va_list ap); int mx_asprintf_forever(char **strp, const char *fmt, ...) __attribute__ ((format(printf, 2, 3))); +char *mx_hostname(void); char *mx_dirname(char *path); char *mx_dirname_forever(char *path); diff --git a/mxq_util.c b/mxq_util.c index b83c64cd..837537a0 100644 --- a/mxq_util.c +++ b/mxq_util.c @@ -19,25 +19,6 @@ #include "mxq_util.h" - -char *mxq_hostname(void) -{ - static char hostname[1024] = ""; - int res; - - if (*hostname) - return hostname; - - res = gethostname(hostname, 1024); - if (res == -1) { - if (errno != ENAMETOOLONG) - assert_perror(errno); - hostname[1024-1] = 0; - } - - return hostname; -} - char **strvec_new(void) { char **strvec; diff --git a/mxq_util.h b/mxq_util.h index e2e85603..8ddbfdda 100644 --- a/mxq_util.h +++ b/mxq_util.h @@ -6,8 +6,6 @@ #include #include -char *mxq_hostname(void); - char** strvec_new(void); size_t strvec_length(char **strvec); int strvec_push_str(char ***strvecp, char *str); diff --git a/mxqd.c b/mxqd.c index bbaf2d1b..7c5886f1 100644 --- a/mxqd.c +++ b/mxqd.c @@ -332,7 +332,7 @@ int server_init(struct mxq_server *server, int argc, char *argv[]) mx_mysql_option_set_default_group(server->mysql, arg_mysql_default_group); mx_mysql_option_set_reconnect(server->mysql, 1); - server->hostname = mxq_hostname(); + server->hostname = mx_hostname(); server->server_id = arg_server_id; server->flock = mx_flock(LOCK_EX, "/dev/shm/mxqd.%s.%s.lck", server->hostname, server->server_id); @@ -768,7 +768,7 @@ static int init_child_process(struct mxq_group_list *group, struct mxq_job *j) mx_setenv_forever("PWD", j->job_workdir); mx_setenv_forever("HOME", passwd->pw_dir); mx_setenv_forever("SHELL", passwd->pw_shell); - mx_setenv_forever("HOSTNAME", mxq_hostname()); + mx_setenv_forever("HOSTNAME", mx_hostname()); mx_setenvf_forever("JOB_ID", "%lu", j->job_id); mx_setenvf_forever("MXQ_JOBID", "%lu", j->job_id); mx_setenvf_forever("MXQ_THREADS", "%d", g->job_threads); diff --git a/mxqsub.c b/mxqsub.c index 979f43d8..d0768fd5 100644 --- a/mxqsub.c +++ b/mxqsub.c @@ -736,7 +736,7 @@ int main(int argc, char *argv[]) /******************************************************************/ group.job_command = arg_program_name; - job.host_submit = mxq_hostname(); + job.host_submit = mx_hostname(); /******************************************************************/