chiark / gitweb /
4d7f32d0a79b562432d46619e2df06b6d6ba3ba0
[elogind.git] / hostname-setup.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include "hostname-setup.h"
29 #include "macro.h"
30 #include "util.h"
31 #include "log.h"
32
33 #define LINE_MAX 4096
34
35 #if defined(TARGET_FEDORA)
36 #define FILENAME "/etc/sysconfig/network"
37 #elif defined(TARGET_SUSE)
38 #define FILENAME "/etc/HOSTNAME"
39 #elif defined(TARGET_DEBIAN)
40 #define FILENAME "/etc/hostname"
41 #elif defined(TARGET_ARCH)
42 #define FILENAME "/etc/rc.conf"
43 #endif
44
45 static int read_hostname(char **hn) {
46
47 #if defined(TARGET_FEDORA) || defined(TARGET_ARCH)
48         int r;
49         FILE *f;
50
51         assert(hn);
52
53         if (!(f = fopen(FILENAME, "re")))
54                 return -errno;
55
56         for (;;) {
57                 char line[LINE_MAX];
58                 char *s, *k;
59
60                 if (!fgets(line, sizeof(line), f)) {
61                         if (feof(f))
62                                 break;
63
64                         r = -errno;
65                         goto finish;
66                 }
67
68                 s = strstrip(line);
69
70                 if (!startswith(s, "HOSTNAME="))
71                         continue;
72
73                 if (!(k = strdup(s+9))) {
74                         r = -ENOMEM;
75                         goto finish;
76                 }
77
78                 *hn = k;
79                 break;
80         }
81
82         r = 0;
83
84 finish:
85         fclose(f);
86         return r;
87
88 #elif defined(TARGET_SUSE) || defined(TARGET_DEBIAN)
89         int r;
90         char *s, *k;
91
92         assert(hn);
93
94         if ((r = read_one_line_file(FILENAME, &s)) < 0)
95                 return r;
96
97         k = strdup(strstrip(s));
98         free(s);
99
100         if (!k)
101                 return -ENOMEM;
102
103         *hn = k;
104
105 #else
106 #warning "Don't know how to read the hostname"
107
108         return -ENOENT;
109 #endif
110
111         return 0;
112 }
113
114 int hostname_setup(void) {
115         int r;
116         char *hn;
117
118         if ((r = read_hostname(&hn)) < 0) {
119                 if (r != -ENOENT)
120                         log_warning("Failed to read configured hostname: %s", strerror(-r));
121
122                 return r;
123         }
124
125         r = sethostname(hn, strlen(hn)) < 0 ? -errno : 0;
126
127         if (r < 0)
128                 log_warning("Failed to set hostname to <%s>: %s", hn, strerror(-r));
129         else
130                 log_info("Set hostname to <%s>.", hn);
131
132         free(hn);
133
134         return r;
135 }