chiark / gitweb /
resolved: use == for comparing unsigned against zero
[elogind.git] / src / resolve / resolved-bus.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 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 "bus-common-errors.h"
23 #include "bus-util.h"
24
25 #include "resolved-dns-domain.h"
26 #include "resolved-bus.h"
27 #include "resolved-def.h"
28
29 static int reply_query_state(DnsQuery *q) {
30         _cleanup_free_ char *ip = NULL;
31         const char *name;
32         int r;
33
34         if (q->request_hostname)
35                 name = q->request_hostname;
36         else {
37                 r = in_addr_to_string(q->request_family, &q->request_address, &ip);
38                 if (r < 0)
39                         return r;
40
41                 name = ip;
42         }
43
44         switch (q->state) {
45
46         case DNS_TRANSACTION_NO_SERVERS:
47                 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
48
49         case DNS_TRANSACTION_TIMEOUT:
50                 return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
51
52         case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED:
53                 return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
54
55         case DNS_TRANSACTION_INVALID_REPLY:
56                 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
57
58         case DNS_TRANSACTION_RESOURCES:
59                 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_RESOURCES, "Not enough resources");
60
61         case DNS_TRANSACTION_ABORTED:
62                 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_ABORTED, "Query aborted");
63
64         case DNS_TRANSACTION_FAILURE: {
65                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
66
67                 if (q->answer_rcode == DNS_RCODE_NXDOMAIN)
68                         sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", name);
69                 else {
70                         const char *rc, *n;
71                         char p[3]; /* the rcode is 4 bits long */
72
73                         rc = dns_rcode_to_string(q->answer_rcode);
74                         if (!rc) {
75                                 sprintf(p, "%i", q->answer_rcode);
76                                 rc = p;
77                         }
78
79                         n = strjoina(_BUS_ERROR_DNS, rc);
80                         sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error %s", name, rc);
81                 }
82
83                 return sd_bus_reply_method_error(q->request, &error);
84         }
85
86         case DNS_TRANSACTION_NULL:
87         case DNS_TRANSACTION_PENDING:
88         case DNS_TRANSACTION_SUCCESS:
89         default:
90                 assert_not_reached("Impossible state");
91         }
92 }
93
94 static int append_address(sd_bus_message *reply, DnsResourceRecord *rr) {
95         int r;
96
97         assert(reply);
98         assert(rr);
99
100         r = sd_bus_message_open_container(reply, 'r', "iay");
101         if (r < 0)
102                 return r;
103
104         if (rr->key->type == DNS_TYPE_A) {
105                 r = sd_bus_message_append(reply, "i", AF_INET);
106                 if (r < 0)
107                         return r;
108
109                 r = sd_bus_message_append_array(reply, 'y', &rr->a.in_addr, sizeof(struct in_addr));
110
111         } else if (rr->key->type == DNS_TYPE_AAAA) {
112                 r = sd_bus_message_append(reply, "i", AF_INET6);
113                 if (r < 0)
114                         return r;
115
116                 r = sd_bus_message_append_array(reply, 'y', &rr->aaaa.in6_addr, sizeof(struct in6_addr));
117         } else
118                 return -EAFNOSUPPORT;
119
120         if (r < 0)
121                 return r;
122
123         r = sd_bus_message_close_container(reply);
124         if (r < 0)
125                 return r;
126
127         return 0;
128 }
129
130 static void bus_method_resolve_hostname_complete(DnsQuery *q) {
131         _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *cname = NULL, *canonical = NULL;
132         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
133         _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
134         unsigned added = 0, i;
135         int r;
136
137         assert(q);
138
139         if (q->state != DNS_TRANSACTION_SUCCESS) {
140                 r = reply_query_state(q);
141                 goto finish;
142         }
143
144         r = sd_bus_message_new_method_return(q->request, &reply);
145         if (r < 0)
146                 goto finish;
147
148         r = sd_bus_message_append(reply, "i", q->answer_ifindex);
149         if (r < 0)
150                 goto finish;
151
152         r = sd_bus_message_open_container(reply, 'a', "(iay)");
153         if (r < 0)
154                 goto finish;
155
156         if (q->answer) {
157                 answer = dns_answer_ref(q->answer);
158
159                 for (i = 0; i < answer->n_rrs; i++) {
160                         r = dns_question_matches_rr(q->question, answer->rrs[i]);
161                         if (r < 0)
162                                 goto finish;
163                         if (r == 0) {
164                                 /* Hmm, if this is not an address record,
165                                    maybe it's a cname? If so, remember this */
166                                 r = dns_question_matches_cname(q->question, answer->rrs[i]);
167                                 if (r < 0)
168                                         goto finish;
169                                 if (r > 0)
170                                         cname = dns_resource_record_ref(answer->rrs[i]);
171
172                                 continue;
173                         }
174
175                         r = append_address(reply, answer->rrs[i]);
176                         if (r < 0)
177                                 goto finish;
178
179                         if (!canonical)
180                                 canonical = dns_resource_record_ref(answer->rrs[i]);
181
182                         added ++;
183                 }
184         }
185
186         if (added == 0) {
187                 if (!cname) {
188                         r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of requested type", q->request_hostname);
189                         goto finish;
190                 }
191
192                 /* This has a cname? Then update the query with the
193                  * new cname. */
194                 r = dns_query_cname_redirect(q, cname->cname.name);
195                 if (r < 0) {
196                         if (r == -ELOOP)
197                                 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop on '%s'", q->request_hostname);
198                         else
199                                 r = sd_bus_reply_method_errno(q->request, -r, NULL);
200
201                         goto finish;
202                 }
203
204                 /* Before we restart the query, let's see if any of
205                  * the RRs we already got already answers our query */
206                 for (i = 0; i < answer->n_rrs; i++) {
207                         r = dns_question_matches_rr(q->question, answer->rrs[i]);
208                         if (r < 0)
209                                 goto finish;
210                         if (r == 0)
211                                 continue;
212
213                         r = append_address(reply, answer->rrs[i]);
214                         if (r < 0)
215                                 goto finish;
216
217                         if (!canonical)
218                                 canonical = dns_resource_record_ref(answer->rrs[i]);
219
220                         added++;
221                 }
222
223                 // what about the cache?
224
225                 /* If we didn't find anything, then let's restart the
226                  * query, this time with the cname */
227                 if (added <= 0) {
228                         r = dns_query_go(q);
229                         if (r == -ESRCH) {
230                                 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
231                                 goto finish;
232                         }
233                         if (r < 0) {
234                                 r = sd_bus_reply_method_errno(q->request, -r, NULL);
235                                 goto finish;
236                         }
237
238                         return;
239                 }
240         }
241
242         r = sd_bus_message_close_container(reply);
243         if (r < 0)
244                 goto finish;
245
246         /* Return the precise spelling and uppercasing reported by the server */
247         assert(canonical);
248         r = sd_bus_message_append(reply, "st", DNS_RESOURCE_KEY_NAME(canonical->key), SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family));
249         if (r < 0)
250                 goto finish;
251
252         r = sd_bus_send(q->manager->bus, reply, NULL);
253
254 finish:
255         if (r < 0) {
256                 log_error_errno(r, "Failed to send hostname reply: %m");
257                 sd_bus_reply_method_errno(q->request, -r, NULL);
258         }
259
260         dns_query_free(q);
261 }
262
263 static int check_ifindex_flags(int ifindex, uint64_t *flags, sd_bus_error *error) {
264         assert(flags);
265
266         if (ifindex < 0)
267                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
268
269         if (*flags & ~SD_RESOLVED_FLAGS_ALL)
270                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
271
272         if (*flags == 0)
273                 *flags = SD_RESOLVED_FLAGS_DEFAULT;
274
275         return 0;
276 }
277
278 static int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
279         _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
280         Manager *m = userdata;
281         const char *hostname;
282         int family, ifindex;
283         uint64_t flags;
284         DnsQuery *q;
285         int r;
286
287         assert(bus);
288         assert(message);
289         assert(m);
290
291         r = sd_bus_message_read(message, "isit", &ifindex, &hostname, &family, &flags);
292         if (r < 0)
293                 return r;
294
295         if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
296                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
297
298         r = dns_name_normalize(hostname, NULL);
299         if (r < 0)
300                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", hostname);
301
302         r = check_ifindex_flags(ifindex, &flags, error);
303         if (r < 0)
304                 return r;
305
306         question = dns_question_new(family == AF_UNSPEC ? 2 : 1);
307         if (!question)
308                 return -ENOMEM;
309
310         if (family != AF_INET6) {
311                 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
312
313                 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, hostname);
314                 if (!key)
315                         return -ENOMEM;
316
317                 r = dns_question_add(question, key);
318                 if (r < 0)
319                         return r;
320         }
321
322         if (family != AF_INET) {
323                 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
324
325                 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, hostname);
326                 if (!key)
327                         return -ENOMEM;
328
329                 r = dns_question_add(question, key);
330                 if (r < 0)
331                         return r;
332         }
333
334         r = dns_query_new(m, &q, question, ifindex, flags);
335         if (r < 0)
336                 return r;
337
338         q->request = sd_bus_message_ref(message);
339         q->request_family = family;
340         q->request_hostname = hostname;
341         q->complete = bus_method_resolve_hostname_complete;
342
343         r = dns_query_bus_track(q, bus, message);
344         if (r < 0)
345                 return r;
346
347         r = dns_query_go(q);
348         if (r < 0) {
349                 dns_query_free(q);
350
351                 if (r == -ESRCH)
352                         sd_bus_error_setf(error, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
353
354                 return r;
355         }
356
357         return 1;
358 }
359
360 static void bus_method_resolve_address_complete(DnsQuery *q) {
361         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
362         _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
363         unsigned added = 0, i;
364         int r;
365
366         assert(q);
367
368         if (q->state != DNS_TRANSACTION_SUCCESS) {
369                 r = reply_query_state(q);
370                 goto finish;
371         }
372
373         r = sd_bus_message_new_method_return(q->request, &reply);
374         if (r < 0)
375                 goto finish;
376
377         r = sd_bus_message_append(reply, "i", q->answer_ifindex);
378         if (r < 0)
379                 goto finish;
380
381         r = sd_bus_message_open_container(reply, 'a', "s");
382         if (r < 0)
383                 goto finish;
384
385         if (q->answer) {
386                 answer = dns_answer_ref(q->answer);
387
388                 for (i = 0; i < answer->n_rrs; i++) {
389                         r = dns_question_matches_rr(q->question, answer->rrs[i]);
390                         if (r < 0)
391                                 goto finish;
392                         if (r == 0)
393                                 continue;
394
395                         r = sd_bus_message_append(reply, "s", answer->rrs[i]->ptr.name);
396                         if (r < 0)
397                                 goto finish;
398
399                         added ++;
400                 }
401         }
402
403         if (added == 0) {
404                 _cleanup_free_ char *ip = NULL;
405
406                 in_addr_to_string(q->request_family, &q->request_address, &ip);
407
408                 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Address '%s' does not have any RR of requested type", ip);
409                 goto finish;
410         }
411
412         r = sd_bus_message_close_container(reply);
413         if (r < 0)
414                 goto finish;
415
416         r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family));
417         if (r < 0)
418                 goto finish;
419
420         r = sd_bus_send(q->manager->bus, reply, NULL);
421
422 finish:
423         if (r < 0) {
424                 log_error_errno(r, "Failed to send address reply: %m");
425                 sd_bus_reply_method_errno(q->request, -r, NULL);
426         }
427
428         dns_query_free(q);
429 }
430
431 static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
432         _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
433         _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
434         _cleanup_free_ char *reverse = NULL;
435         Manager *m = userdata;
436         int family, ifindex;
437         uint64_t flags;
438         const void *d;
439         DnsQuery *q;
440         size_t sz;
441         int r;
442
443         assert(bus);
444         assert(message);
445         assert(m);
446
447         r = sd_bus_message_read(message, "ii", &ifindex, &family);
448         if (r < 0)
449                 return r;
450
451         if (!IN_SET(family, AF_INET, AF_INET6))
452                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
453
454         r = sd_bus_message_read_array(message, 'y', &d, &sz);
455         if (r < 0)
456                 return r;
457
458         if (sz != FAMILY_ADDRESS_SIZE(family))
459                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid address size");
460
461         r = sd_bus_message_read(message, "t", &flags);
462         if (r < 0)
463                 return r;
464
465         r = check_ifindex_flags(ifindex, &flags, error);
466         if (r < 0)
467                 return r;
468
469         r = dns_name_reverse(family, d, &reverse);
470         if (r < 0)
471                 return r;
472
473         question = dns_question_new(1);
474         if (!question)
475                 return -ENOMEM;
476
477         key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, reverse);
478         if (!key)
479                 return -ENOMEM;
480
481         reverse = NULL;
482
483         r = dns_question_add(question, key);
484         if (r < 0)
485                 return r;
486
487         r = dns_query_new(m, &q, question, ifindex, flags);
488         if (r < 0)
489                 return r;
490
491         q->request = sd_bus_message_ref(message);
492         q->request_family = family;
493         memcpy(&q->request_address, d, sz);
494         q->complete = bus_method_resolve_address_complete;
495
496         r = dns_query_bus_track(q, bus, message);
497         if (r < 0)
498                 return r;
499
500         r = dns_query_go(q);
501         if (r < 0) {
502                 dns_query_free(q);
503
504                 if (r == -ESRCH)
505                         sd_bus_error_setf(error, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
506
507                 return r;
508         }
509
510         return 1;
511 }
512
513 static void bus_method_resolve_record_complete(DnsQuery *q) {
514         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
515         _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
516         unsigned added = 0, i;
517         int r;
518
519         assert(q);
520
521         if (q->state != DNS_TRANSACTION_SUCCESS) {
522                 r = reply_query_state(q);
523                 goto finish;
524         }
525
526         r = sd_bus_message_new_method_return(q->request, &reply);
527         if (r < 0)
528                 goto finish;
529
530         r = sd_bus_message_append(reply, "i", q->answer_ifindex);
531         if (r < 0)
532                 goto finish;
533
534         r = sd_bus_message_open_container(reply, 'a', "(qqay)");
535         if (r < 0)
536                 goto finish;
537
538         if (q->answer) {
539                 answer = dns_answer_ref(q->answer);
540
541                 for (i = 0; i < answer->n_rrs; i++) {
542                         _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
543                         size_t start;
544
545                         r = dns_question_matches_rr(q->question, answer->rrs[i]);
546                         if (r < 0)
547                                 goto finish;
548                         if (r == 0)
549                                 continue;
550
551                         r = dns_packet_new(&p, DNS_PROTOCOL_DNS, 0);
552                         if (r < 0)
553                                 goto finish;
554
555                         r = dns_packet_append_rr(p, answer->rrs[i], &start);
556                         if (r < 0)
557                                 goto finish;
558
559                         r = sd_bus_message_open_container(reply, 'r', "qqay");
560                         if (r < 0)
561                                 goto finish;
562
563                         r = sd_bus_message_append(reply, "qq", answer->rrs[i]->key->class, answer->rrs[i]->key->type);
564                         if (r < 0)
565                                 goto finish;
566
567                         r = sd_bus_message_append_array(reply, 'y', DNS_PACKET_DATA(p) + start, p->size - start);
568                         if (r < 0)
569                                 goto finish;
570
571                         r = sd_bus_message_close_container(reply);
572                         if (r < 0)
573                                 goto finish;
574
575                         added ++;
576                 }
577         }
578
579         if (added <= 0) {
580                 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Name '%s' does not have any RR of the requested type", q->request_hostname);
581                 goto finish;
582         }
583
584         r = sd_bus_message_close_container(reply);
585         if (r < 0)
586                 goto finish;
587
588         r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family));
589         if (r < 0)
590                 goto finish;
591
592         r = sd_bus_send(q->manager->bus, reply, NULL);
593
594 finish:
595         if (r < 0) {
596                 log_error_errno(r, "Failed to send record reply: %m");
597                 sd_bus_reply_method_errno(q->request, -r, NULL);
598         }
599
600         dns_query_free(q);
601 }
602
603 static int bus_method_resolve_record(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
604         _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
605         _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
606         Manager *m = userdata;
607         uint16_t class, type;
608         const char *name;
609         int r, ifindex;
610         uint64_t flags;
611         DnsQuery *q;
612
613         assert(bus);
614         assert(message);
615         assert(m);
616
617         r = sd_bus_message_read(message, "isqqt", &ifindex, &name, &class, &type, &flags);
618         if (r < 0)
619                 return r;
620
621         r = dns_name_normalize(name, NULL);
622         if (r < 0)
623                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name);
624
625         r = check_ifindex_flags(ifindex, &flags, error);
626         if (r < 0)
627                 return r;
628
629         question = dns_question_new(1);
630         if (!question)
631                 return -ENOMEM;
632
633         key = dns_resource_key_new(class, type, name);
634         if (!key)
635                 return -ENOMEM;
636
637         r = dns_question_add(question, key);
638         if (r < 0)
639                 return r;
640
641         r = dns_query_new(m, &q, question, ifindex, flags);
642         if (r < 0)
643                 return r;
644
645         q->request = sd_bus_message_ref(message);
646         q->request_hostname = name;
647         q->complete = bus_method_resolve_record_complete;
648
649         r = dns_query_bus_track(q, bus, message);
650         if (r < 0)
651                 return r;
652
653         r = dns_query_go(q);
654         if (r < 0) {
655                 dns_query_free(q);
656
657                 if (r == -ESRCH)
658                         sd_bus_error_setf(error, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
659
660                 return r;
661         }
662
663         return 1;
664 }
665
666 static const sd_bus_vtable resolve_vtable[] = {
667         SD_BUS_VTABLE_START(0),
668         SD_BUS_METHOD("ResolveHostname", "isit", "ia(iay)st", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
669         SD_BUS_METHOD("ResolveAddress", "iiayt", "iast", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED),
670         SD_BUS_METHOD("ResolveRecord", "isqqt", "ia(qqay)t", bus_method_resolve_record, SD_BUS_VTABLE_UNPRIVILEGED),
671         SD_BUS_VTABLE_END,
672 };
673
674 static int on_bus_retry(sd_event_source *s, usec_t usec, void *userdata) {
675         Manager *m = userdata;
676
677         assert(s);
678         assert(m);
679
680         m->bus_retry_event_source = sd_event_source_unref(m->bus_retry_event_source);
681
682         manager_connect_bus(m);
683         return 0;
684 }
685
686 static int match_prepare_for_sleep(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
687         Manager *m = userdata;
688         int b, r;
689
690         assert(bus);
691         assert(bus);
692
693         r = sd_bus_message_read(message, "b", &b);
694         if (r < 0) {
695                 log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m");
696                 return 0;
697         }
698
699         if (b)
700                 return 0;
701
702         log_debug("Coming back from suspend, verifying all RRs...");
703
704         manager_verify_all(m);
705         return 0;
706 }
707
708 int manager_connect_bus(Manager *m) {
709         int r;
710
711         assert(m);
712
713         if (m->bus)
714                 return 0;
715
716         r = sd_bus_default_system(&m->bus);
717         if (r < 0) {
718                 /* We failed to connect? Yuck, we must be in early
719                  * boot. Let's try in 5s again. As soon as we have
720                  * kdbus we can stop doing this... */
721
722                 log_debug_errno(r, "Failed to connect to bus, trying again in 5s: %m");
723
724                 r = sd_event_add_time(m->event, &m->bus_retry_event_source, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 5*USEC_PER_SEC, 0, on_bus_retry, m);
725                 if (r < 0)
726                         return log_error_errno(r, "Failed to install bus reconnect time event: %m");
727
728                 return 0;
729         }
730
731         r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/resolve1", "org.freedesktop.resolve1.Manager", resolve_vtable, m);
732         if (r < 0)
733                 return log_error_errno(r, "Failed to register object: %m");
734
735         r = sd_bus_request_name(m->bus, "org.freedesktop.resolve1", 0);
736         if (r < 0)
737                 return log_error_errno(r, "Failed to register name: %m");
738
739         r = sd_bus_attach_event(m->bus, m->event, 0);
740         if (r < 0)
741                 return log_error_errno(r, "Failed to attach bus to event loop: %m");
742
743         r = sd_bus_add_match(m->bus, &m->prepare_for_sleep_slot,
744                              "type='signal',"
745                              "sender='org.freedesktop.login1',"
746                              "interface='org.freedesktop.login1.Manager',"
747                              "member='PrepareForSleep',"
748                              "path='/org/freedesktop/login1'",
749                              match_prepare_for_sleep,
750                              m);
751         if (r < 0)
752                 log_error_errno(r, "Failed to add match for PrepareForSleep: %m");
753
754         return 0;
755 }