]>
Commit | Line | Data |
---|---|---|
302e8c4c LP |
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 | ||
302e8c4c | 35 | #if defined(TARGET_FEDORA) |
ea6145da LP |
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" | |
3177a7fa MAP |
43 | #elif defined(TARGET_GENTOO) |
44 | #define FILENAME "/etc/conf.d/hostname" | |
ea6145da | 45 | #endif |
302e8c4c | 46 | |
ea6145da | 47 | static int read_hostname(char **hn) { |
302e8c4c | 48 | |
3177a7fa | 49 | #if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO) |
d7c114c0 DR |
50 | int r; |
51 | FILE *f; | |
52 | ||
53 | assert(hn); | |
54 | ||
ea6145da | 55 | if (!(f = fopen(FILENAME, "re"))) |
d7c114c0 DR |
56 | return -errno; |
57 | ||
58 | for (;;) { | |
59 | char line[LINE_MAX]; | |
60 | char *s, *k; | |
61 | ||
62 | if (!fgets(line, sizeof(line), f)) { | |
63 | if (feof(f)) | |
64 | break; | |
65 | ||
66 | r = -errno; | |
67 | goto finish; | |
68 | } | |
69 | ||
70 | s = strstrip(line); | |
71 | ||
3177a7fa | 72 | if (!startswith_no_case(s, "HOSTNAME=")) |
d7c114c0 DR |
73 | continue; |
74 | ||
75 | if (!(k = strdup(s+9))) { | |
76 | r = -ENOMEM; | |
77 | goto finish; | |
78 | } | |
79 | ||
3177a7fa MAP |
80 | if (!(k = delete_chars(k, "\"\'"))) { |
81 | r = -ENOMEM; | |
82 | goto finish; | |
83 | } | |
84 | ||
d7c114c0 DR |
85 | *hn = k; |
86 | break; | |
87 | } | |
88 | ||
89 | r = 0; | |
90 | ||
91 | finish: | |
92 | fclose(f); | |
93 | return r; | |
94 | ||
ea6145da | 95 | #elif defined(TARGET_SUSE) || defined(TARGET_DEBIAN) |
206bf5c2 KS |
96 | int r; |
97 | char *s, *k; | |
98 | ||
99 | assert(hn); | |
100 | ||
ea6145da | 101 | if ((r = read_one_line_file(FILENAME, &s)) < 0) |
206bf5c2 KS |
102 | return r; |
103 | ||
104 | k = strdup(strstrip(s)); | |
105 | free(s); | |
106 | ||
107 | if (!k) | |
108 | return -ENOMEM; | |
109 | ||
110 | *hn = k; | |
111 | ||
302e8c4c | 112 | #else |
206bf5c2 | 113 | #warning "Don't know how to read the hostname" |
302e8c4c LP |
114 | |
115 | return -ENOENT; | |
116 | #endif | |
117 | ||
118 | return 0; | |
119 | } | |
120 | ||
121 | int hostname_setup(void) { | |
122 | int r; | |
123 | char *hn; | |
124 | ||
125 | if ((r = read_hostname(&hn)) < 0) { | |
126 | if (r != -ENOENT) | |
127 | log_warning("Failed to read configured hostname: %s", strerror(-r)); | |
128 | ||
129 | return r; | |
130 | } | |
131 | ||
132 | r = sethostname(hn, strlen(hn)) < 0 ? -errno : 0; | |
133 | ||
134 | if (r < 0) | |
135 | log_warning("Failed to set hostname to <%s>: %s", hn, strerror(-r)); | |
136 | else | |
137 | log_info("Set hostname to <%s>.", hn); | |
138 | ||
139 | free(hn); | |
140 | ||
141 | return r; | |
142 | } |