]> git.ipfire.org Git - people/ms/systemd.git/blame - hostname-setup.c
swap: add .swap unit type
[people/ms/systemd.git] / hostname-setup.c
CommitLineData
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"
43#endif
302e8c4c 44
ea6145da 45static int read_hostname(char **hn) {
302e8c4c 46
ea6145da 47#if defined(TARGET_FEDORA) || defined(TARGET_ARCH)
d7c114c0
DR
48 int r;
49 FILE *f;
50
51 assert(hn);
52
ea6145da 53 if (!(f = fopen(FILENAME, "re")))
d7c114c0
DR
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
84finish:
85 fclose(f);
86 return r;
87
ea6145da 88#elif defined(TARGET_SUSE) || defined(TARGET_DEBIAN)
206bf5c2
KS
89 int r;
90 char *s, *k;
91
92 assert(hn);
93
ea6145da 94 if ((r = read_one_line_file(FILENAME, &s)) < 0)
206bf5c2
KS
95 return r;
96
97 k = strdup(strstrip(s));
98 free(s);
99
100 if (!k)
101 return -ENOMEM;
102
103 *hn = k;
104
302e8c4c 105#else
206bf5c2 106#warning "Don't know how to read the hostname"
302e8c4c
LP
107
108 return -ENOENT;
109#endif
110
111 return 0;
112}
113
114int 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}