]> git.meshlink.io Git - catta/blob - avahi-daemon/static-services.c
Fix service reloading on SIGHUP (thanks, Sebastien!)
[catta] / avahi-daemon / static-services.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/stat.h>
27 #include <glob.h>
28 #include <limits.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33
34 #include <expat.h>
35
36 #include <avahi-common/llist.h>
37 #include <avahi-common/malloc.h>
38 #include <avahi-core/log.h>
39
40 #include "main.h"
41 #include "static-services.h"
42
43 typedef struct StaticService StaticService;
44 typedef struct StaticServiceGroup StaticServiceGroup;
45
46 struct StaticService {
47     StaticServiceGroup *group;
48     
49     char *type;
50     char *domain_name;
51     char *host_name;
52     uint16_t port;
53
54     AvahiStringList *txt_records;
55     
56     AVAHI_LLIST_FIELDS(StaticService, services);
57 };
58
59 struct StaticServiceGroup {
60     char *filename;
61     time_t mtime;
62
63     char *name, *chosen_name;
64     int replace_wildcards;
65
66     AvahiSEntryGroup *entry_group;
67     AVAHI_LLIST_HEAD(StaticService, services);
68     AVAHI_LLIST_FIELDS(StaticServiceGroup, groups);
69 };
70
71 static AVAHI_LLIST_HEAD(StaticServiceGroup, groups) = NULL;
72
73 static char *replacestr(const char *pattern, const char *a, const char *b) {
74     char *r = NULL, *e, *n;
75
76     while ((e = strstr(pattern, a))) {
77         char *k;
78
79         k = avahi_strndup(pattern, e - pattern);
80         if (r)
81             n = avahi_strdup_printf("%s%s%s", r, k, b);
82         else
83             n = avahi_strdup_printf("%s%s", k, b);
84
85         avahi_free(k);
86         avahi_free(r);
87         r = n;
88
89         pattern = e + strlen(a);
90     }
91
92     if (!r)
93         return avahi_strdup(pattern);
94
95     n = avahi_strdup_printf("%s%s", r, pattern);
96     avahi_free(r);
97
98     return n;
99 }
100
101 static void add_static_service_group_to_server(StaticServiceGroup *g);
102 static void remove_static_service_group_from_server(StaticServiceGroup *g);
103
104 static StaticService *static_service_new(StaticServiceGroup *group) {
105     StaticService *s;
106     
107     assert(group);
108     s = avahi_new(StaticService, 1);
109     s->group = group;
110
111     s->type = s->host_name = s->domain_name = NULL;
112     s->port = 0;
113
114     s->txt_records = NULL;
115
116     AVAHI_LLIST_PREPEND(StaticService, services, group->services, s);
117
118     return s;
119 }
120
121 static StaticServiceGroup *static_service_group_new(char *filename) {
122     StaticServiceGroup *g;
123     assert(filename);
124
125     g = avahi_new(StaticServiceGroup, 1);
126     g->filename = avahi_strdup(filename);
127     g->mtime = 0;
128     g->name = g->chosen_name = NULL;
129     g->replace_wildcards = 0;
130     g->entry_group = NULL;
131
132     AVAHI_LLIST_HEAD_INIT(StaticService, g->services);
133     AVAHI_LLIST_PREPEND(StaticServiceGroup, groups, groups, g);
134
135     return g;
136 }
137
138 static void static_service_free(StaticService *s) {
139     assert(s);
140     
141     AVAHI_LLIST_REMOVE(StaticService, services, s->group->services, s);
142
143     avahi_free(s->type);
144     avahi_free(s->host_name);
145     avahi_free(s->domain_name);
146
147     avahi_string_list_free(s->txt_records);
148     
149     avahi_free(s);
150 }
151
152 static void static_service_group_free(StaticServiceGroup *g) {
153     assert(g);
154
155     if (g->entry_group)
156         avahi_s_entry_group_free(g->entry_group);
157
158     while (g->services)
159         static_service_free(g->services);
160
161     AVAHI_LLIST_REMOVE(StaticServiceGroup, groups, groups, g);
162
163     avahi_free(g->filename);
164     avahi_free(g->name);
165     avahi_free(g->chosen_name);
166     avahi_free(g);
167 }
168
169 static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *eg, AvahiEntryGroupState state, void* userdata) {
170     StaticServiceGroup *g = userdata;
171     
172     assert(s);
173     assert(g);
174     
175     if (state == AVAHI_ENTRY_GROUP_COLLISION) {
176         char *n;
177
178         remove_static_service_group_from_server(g);
179
180         n = avahi_alternative_service_name(g->chosen_name);
181         avahi_free(g->chosen_name);
182         g->chosen_name = n;
183
184         avahi_log_notice("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
185
186         add_static_service_group_to_server(g);
187     } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
188         avahi_log_info("Service \"%s\" (%s) successfully established.", g->chosen_name, g->filename);
189 }
190
191 static void add_static_service_group_to_server(StaticServiceGroup *g) {
192     StaticService *s;
193
194     assert(g);
195
196     if (g->entry_group && !avahi_s_entry_group_is_empty(g->entry_group))
197         /* This service group is already registered in the server */
198         return;
199     
200     if (g->chosen_name)
201         avahi_free(g->chosen_name);
202     
203     if (g->replace_wildcards)
204         g->chosen_name = replacestr(g->name, "%h", avahi_server_get_host_name(avahi_server));
205     else
206         g->chosen_name = avahi_strdup(g->name);
207
208     if (!g->entry_group)
209         g->entry_group = avahi_s_entry_group_new(avahi_server, entry_group_callback, g);
210
211     assert(avahi_s_entry_group_is_empty(g->entry_group));
212     
213     for (s = g->services; s; s = s->services_next) {
214
215         if (avahi_server_add_service_strlst(
216                 avahi_server,
217                 g->entry_group,
218                 -1, AF_UNSPEC,
219                 g->chosen_name, s->type, 
220                 s->domain_name, s->host_name, s->port,
221                 s->txt_records) < 0) {
222             avahi_log_error("Failed to add service '%s' of type '%s', ignoring service group (%s): %s",
223                             g->chosen_name, s->type, g->filename,
224                             avahi_strerror(avahi_server_errno(avahi_server)));
225             remove_static_service_group_from_server(g);
226             return;
227         }
228     }
229
230     avahi_s_entry_group_commit(g->entry_group);
231 }
232
233 static void remove_static_service_group_from_server(StaticServiceGroup *g) {
234     assert(g);
235
236     if (g->entry_group)
237         avahi_s_entry_group_reset(g->entry_group);
238 }
239
240 typedef enum {
241     XML_TAG_INVALID,
242     XML_TAG_SERVICE_GROUP,
243     XML_TAG_NAME,
244     XML_TAG_SERVICE,
245     XML_TAG_TYPE,
246     XML_TAG_DOMAIN_NAME,
247     XML_TAG_HOST_NAME,
248     XML_TAG_PORT,
249     XML_TAG_TXT_RECORD
250 } xml_tag_name;
251
252 struct xml_userdata {
253     StaticServiceGroup *group;
254     StaticService *service;
255     xml_tag_name current_tag;
256     int failed;
257     char *buf;
258 };
259
260 static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
261     struct xml_userdata *u = data;
262     
263     assert(u);
264
265     if (u->failed)
266         return;
267
268     if (u->current_tag == XML_TAG_INVALID && strcmp(el, "service-group") == 0) {
269
270         if (attr[0])
271             goto invalid_attr;
272
273         u->current_tag = XML_TAG_SERVICE_GROUP;
274     } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "name") == 0) {
275         u->current_tag = XML_TAG_NAME;
276
277         if (attr[0]) {
278             if (strcmp(attr[0], "replace-wildcards") == 0) 
279                 u->group->replace_wildcards = strcmp(attr[1], "yes") == 0;
280             else
281                 goto invalid_attr;
282         }
283
284         if (attr[2])
285             goto invalid_attr;
286         
287     } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "service") == 0) {
288         if (attr[0])
289             goto invalid_attr;
290
291         assert(!u->service);
292         u->service = static_service_new(u->group);
293
294         u->current_tag = XML_TAG_SERVICE;
295     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "type") == 0) {
296         if (attr[0])
297             goto invalid_attr;
298
299         u->current_tag = XML_TAG_TYPE;
300     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "domain-name") == 0) {
301         if (attr[0])
302             goto invalid_attr;
303         
304         u->current_tag = XML_TAG_DOMAIN_NAME;
305     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "host-name") == 0) {
306         if (attr[0])
307             goto invalid_attr;
308         
309         u->current_tag = XML_TAG_HOST_NAME;
310     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "port") == 0) {
311         if (attr[0])
312             goto invalid_attr;
313         
314         u->current_tag = XML_TAG_PORT;
315     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "txt-record") == 0) {
316         if (attr[0])
317             goto invalid_attr;
318         
319         u->current_tag = XML_TAG_TXT_RECORD;
320     } else {
321         avahi_log_error("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
322         u->failed = 1;
323     }
324
325     return;
326
327 invalid_attr:
328     avahi_log_error("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
329     u->failed = 1;
330     return;
331 }
332     
333 static void XMLCALL xml_end(void *data, const char *el) {
334     struct xml_userdata *u = data;
335     assert(u);
336
337     if (u->failed)
338         return;
339     
340     switch (u->current_tag) {
341         case XML_TAG_SERVICE_GROUP:
342
343             if (!u->group->name || !u->group->services) {
344                 avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename);
345                 u->failed = 1;
346                 return;
347             }
348             
349             u->current_tag = XML_TAG_INVALID;
350             break;
351
352         case XML_TAG_SERVICE:
353
354             if (!u->service->type) {
355                 avahi_log_error("%s: parse failure: service incomplete.", u->group->filename);
356                 u->failed = 1;
357                 return;
358             }
359             
360             u->service = NULL;
361             u->current_tag = XML_TAG_SERVICE_GROUP;
362             break;
363
364         case XML_TAG_NAME:
365             u->current_tag = XML_TAG_SERVICE_GROUP;
366             break;
367
368         case XML_TAG_PORT: {
369             int p;
370             assert(u->service);
371             
372             p = u->buf ? atoi(u->buf) : 0;
373
374             if (p < 0 || p > 0xFFFF) {
375                 avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
376                 u->failed = 1;
377                 return;
378             }
379
380             u->service->port = (uint16_t) p;
381
382             u->current_tag = XML_TAG_SERVICE;
383             break;
384         }
385
386         case XML_TAG_TXT_RECORD: {
387             assert(u->service);
388             
389             u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : "");
390             u->current_tag = XML_TAG_SERVICE;
391             break;
392         }
393             
394         case XML_TAG_TYPE:
395         case XML_TAG_DOMAIN_NAME:
396         case XML_TAG_HOST_NAME:
397             u->current_tag = XML_TAG_SERVICE;
398             break;
399
400         case XML_TAG_INVALID:
401             ;
402     }
403
404     avahi_free(u->buf);
405     u->buf = NULL;
406 }
407
408 static char *append_cdata(char *t, const char *n, int length) {
409     char *r, *k;
410     
411     if (!length)
412         return t;
413
414     
415     k = avahi_strndup(n, length);
416
417     if (t) {
418         r = avahi_strdup_printf("%s%s", t, k);
419         avahi_free(k);
420         avahi_free(t);
421     } else
422         r = k;
423     
424     return r;
425 }
426
427 static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
428     struct xml_userdata *u = data;
429     assert(u);
430
431     if (u->failed)
432         return;
433
434     switch (u->current_tag) {
435         case XML_TAG_NAME:
436             u->group->name = append_cdata(u->group->name, s, len);
437             break;
438             
439         case XML_TAG_TYPE:
440             assert(u->service);
441             u->service->type = append_cdata(u->service->type, s, len);
442             break;
443
444         case XML_TAG_DOMAIN_NAME:
445             assert(u->service);
446             u->service->domain_name = append_cdata(u->service->domain_name, s, len);
447             break;
448
449         case XML_TAG_HOST_NAME:
450             assert(u->service);
451             u->service->host_name = append_cdata(u->service->host_name, s, len);
452             break;
453
454         case XML_TAG_PORT:
455         case XML_TAG_TXT_RECORD:
456             u->buf = append_cdata(u->buf, s, len);
457             break;
458
459         case XML_TAG_SERVICE_GROUP:
460         case XML_TAG_SERVICE:
461         case XML_TAG_INVALID:
462             ;
463     }
464 }
465
466 static int static_service_group_load(StaticServiceGroup *g) {
467     XML_Parser parser = NULL;
468     int fd = -1;
469     struct xml_userdata u;
470     int r = -1;
471     struct stat st;
472     ssize_t n;
473
474     assert(g);
475
476     u.buf = NULL;
477     u.group = g;
478     u.service = NULL;
479     u.current_tag = XML_TAG_INVALID;
480     u.failed = 0;
481
482     /* Cleanup old data in this service group, if available */
483     remove_static_service_group_from_server(g);
484     while (g->services)
485         static_service_free(g->services);
486
487     avahi_free(g->name);
488     avahi_free(g->chosen_name);
489     g->name = g->chosen_name = NULL;
490     g->replace_wildcards = 0;
491     
492     if (!(parser = XML_ParserCreate(NULL))) {
493         avahi_log_error("XML_ParserCreate() failed.");
494         goto finish;
495     }
496
497     if ((fd = open(g->filename, O_RDONLY)) < 0) {
498         avahi_log_error("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
499         goto finish;
500     }
501
502     if (fstat(fd, &st) < 0) {
503         avahi_log_error("fstat(): %s", strerror(errno));
504         goto finish;
505     }
506
507     g->mtime = st.st_mtime;
508     
509     XML_SetUserData(parser, &u);
510
511     XML_SetElementHandler(parser, xml_start, xml_end);
512     XML_SetCharacterDataHandler(parser, xml_cdata);
513
514     do {
515         void *buffer;
516
517 #define BUFSIZE (10*1024)
518
519         if (!(buffer = XML_GetBuffer(parser, BUFSIZE))) {
520             avahi_log_error("XML_GetBuffer() failed.");
521             goto finish;
522         }
523
524         if ((n = read(fd, buffer, BUFSIZE)) < 0) {
525             avahi_log_error("read(): %s\n", strerror(errno));
526             goto finish;
527         }
528
529         if (!XML_ParseBuffer(parser, n, n == 0)) {
530             avahi_log_error("XML_ParseBuffer() failed at line %d: %s.\n", XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
531             goto finish;
532         }
533
534     } while (n != 0);
535
536     if (!u.failed)
537         r = 0;
538
539 finish:
540
541     if (fd >= 0)
542         close(fd);
543
544     if (parser)
545         XML_ParserFree(parser);
546
547     avahi_free(u.buf);
548
549     return r;
550 }
551
552 static void load_file(char *n) {
553     StaticServiceGroup *g;
554     assert(n);
555
556     for (g = groups; g; g = g->groups_next)
557         if (strcmp(g->filename, n) == 0)
558             return;
559
560     avahi_log_info("Loading service file %s", n);
561     
562     g = static_service_group_new(n);
563     if (static_service_group_load(g) < 0) {
564         avahi_log_error("Failed to load service group file %s, ignoring.", g->filename);
565         static_service_group_free(g);
566     }
567 }
568
569 void static_service_load(void) {
570     StaticServiceGroup *g, *n;
571     glob_t globbuf;
572     char **p;
573
574     for (g = groups; g; g = n) {
575         struct stat st;
576
577         n = g->groups_next;
578
579         if (stat(g->filename, &st) < 0) {
580
581             if (errno == ENOENT)
582                 avahi_log_info("Service group file %s vanished, removing services.", g->filename);
583             else
584                 avahi_log_warn("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
585             
586             static_service_group_free(g);
587         } else if (st.st_mtime != g->mtime) {
588             avahi_log_info("Service group file %s changed, reloading.", g->filename);
589             
590             if (static_service_group_load(g) < 0) {
591                 avahi_log_warn("Failed to load service group file %s, removing service.", g->filename);
592                 static_service_group_free(g);
593             }
594         }
595     }
596
597     memset(&globbuf, 0, sizeof(globbuf));
598     if (glob(AVAHI_SERVICE_DIR "/*.service", GLOB_ERR, NULL, &globbuf) != 0)
599         avahi_log_error("Failed to read service directory.");
600     else {
601         for (p = globbuf.gl_pathv; *p; p++)
602             load_file(*p);
603         
604         globfree(&globbuf);
605     }
606 }
607
608 void static_service_free_all(void) {
609     
610     while (groups)
611         static_service_group_free(groups);
612 }
613
614 void static_service_add_to_server(void) {
615     StaticServiceGroup *g;
616
617     for (g = groups; g; g = g->groups_next)
618         add_static_service_group_to_server(g);
619 }
620
621 void static_service_remove_from_server(void) {
622     StaticServiceGroup *g;
623
624     for (g = groups; g; g = g->groups_next)
625         remove_static_service_group_from_server(g);
626 }