]> git.proxmox.com Git - systemd.git/blob - src/timesync/timesyncd-server.c
Enable seccomp support on powerpc, ppc64el, and s390x
[systemd.git] / src / timesync / timesyncd-server.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Kay Sievers, Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "alloc-util.h"
23 #include "timesyncd-server.h"
24
25 int server_address_new(
26 ServerName *n,
27 ServerAddress **ret,
28 const union sockaddr_union *sockaddr,
29 socklen_t socklen) {
30
31 ServerAddress *a, *tail;
32
33 assert(n);
34 assert(sockaddr);
35 assert(socklen >= offsetof(struct sockaddr, sa_data));
36 assert(socklen <= sizeof(union sockaddr_union));
37
38 a = new0(ServerAddress, 1);
39 if (!a)
40 return -ENOMEM;
41
42 memcpy(&a->sockaddr, sockaddr, socklen);
43 a->socklen = socklen;
44
45 LIST_FIND_TAIL(addresses, n->addresses, tail);
46 LIST_INSERT_AFTER(addresses, n->addresses, tail, a);
47 a->name = n;
48
49 if (ret)
50 *ret = a;
51
52 return 0;
53 }
54
55 ServerAddress* server_address_free(ServerAddress *a) {
56 if (!a)
57 return NULL;
58
59 if (a->name) {
60 LIST_REMOVE(addresses, a->name->addresses, a);
61
62 if (a->name->manager && a->name->manager->current_server_address == a)
63 manager_set_server_address(a->name->manager, NULL);
64 }
65
66 free(a);
67 return NULL;
68 }
69
70 int server_name_new(
71 Manager *m,
72 ServerName **ret,
73 ServerType type,
74 const char *string) {
75
76 ServerName *n, *tail;
77
78 assert(m);
79 assert(string);
80
81 n = new0(ServerName, 1);
82 if (!n)
83 return -ENOMEM;
84
85 n->type = type;
86 n->string = strdup(string);
87 if (!n->string) {
88 free(n);
89 return -ENOMEM;
90 }
91
92 if (type == SERVER_SYSTEM) {
93 LIST_FIND_TAIL(names, m->system_servers, tail);
94 LIST_INSERT_AFTER(names, m->system_servers, tail, n);
95 } else if (type == SERVER_LINK) {
96 LIST_FIND_TAIL(names, m->link_servers, tail);
97 LIST_INSERT_AFTER(names, m->link_servers, tail, n);
98 } else if (type == SERVER_FALLBACK) {
99 LIST_FIND_TAIL(names, m->fallback_servers, tail);
100 LIST_INSERT_AFTER(names, m->fallback_servers, tail, n);
101 } else
102 assert_not_reached("Unknown server type");
103
104 n->manager = m;
105
106 if (type != SERVER_FALLBACK &&
107 m->current_server_name &&
108 m->current_server_name->type == SERVER_FALLBACK)
109 manager_set_server_name(m, NULL);
110
111 log_debug("Added new server %s.", string);
112
113 if (ret)
114 *ret = n;
115
116 return 0;
117 }
118
119 ServerName *server_name_free(ServerName *n) {
120 if (!n)
121 return NULL;
122
123 server_name_flush_addresses(n);
124
125 if (n->manager) {
126 if (n->type == SERVER_SYSTEM)
127 LIST_REMOVE(names, n->manager->system_servers, n);
128 else if (n->type == SERVER_LINK)
129 LIST_REMOVE(names, n->manager->link_servers, n);
130 else if (n->type == SERVER_FALLBACK)
131 LIST_REMOVE(names, n->manager->fallback_servers, n);
132 else
133 assert_not_reached("Unknown server type");
134
135 if (n->manager->current_server_name == n)
136 manager_set_server_name(n->manager, NULL);
137 }
138
139 log_debug("Removed server %s.", n->string);
140
141 free(n->string);
142 free(n);
143
144 return NULL;
145 }
146
147 void server_name_flush_addresses(ServerName *n) {
148 assert(n);
149
150 while (n->addresses)
151 server_address_free(n->addresses);
152 }