along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include <unistd.h>
-#include <stdio.h>
#include <errno.h>
-#include <string.h>
+#include <stdio.h>
#include <stdlib.h>
-#include "hostname-setup.h"
+#include "fileio.h"
+#include "hostname-util.h"
+#include "log.h"
#include "macro.h"
+#include "string-util.h"
#include "util.h"
-#include "log.h"
-#include "fileio.h"
-
-static int read_and_strip_hostname(const char *path, char **hn) {
- char *s;
- int r;
-
- assert(path);
- assert(hn);
-
- r = read_one_line_file(path, &s);
- if (r < 0)
- return r;
-
- hostname_cleanup(s);
-
- if (isempty(s)) {
- free(s);
- return -ENOENT;
- }
-
- *hn = s;
- return 0;
-}
+#include "hostname-setup.h"
int hostname_setup(void) {
int r;
const char *hn;
bool enoent = false;
- r = read_and_strip_hostname("/etc/hostname", &b);
+ r = read_hostname_config("/etc/hostname", &b);
if (r < 0) {
if (r == -ENOENT)
enoent = true;
else
- log_warning("Failed to read configured hostname: %s", strerror(-r));
+ log_warning_errno(r, "Failed to read configured hostname: %m");
hn = NULL;
} else
hn = "localhost";
}
- if (sethostname(hn, strlen(hn)) < 0) {
- log_warning("Failed to set hostname to <%s>: %m", hn);
- return -errno;
- }
+ if (sethostname_idempotent(hn) < 0)
+ return log_warning_errno(errno, "Failed to set hostname to <%s>: %m", hn);
log_info("Set hostname to <%s>.", hn);
return 0;