]> git.meshlink.io Git - catta/blob - avahi-compat-libdns_sd/warn.c
* integrate avahi-compat-howl into build system
[catta] / avahi-compat-libdns_sd / warn.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 <pthread.h>
27 #include <unistd.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <assert.h>
33 #include <stdarg.h>
34 #include <syslog.h>
35
36 #include "warn.h"
37
38 #ifndef COMPAT_LAYER
39 #define COMPAT_LAYER "Apple Bonjour"
40 #endif 
41
42 static pthread_mutex_t linkage_mutex = PTHREAD_MUTEX_INITIALIZER;
43 static int linkage_warning = 0;
44
45 static void get_exe_name(char *t, size_t l) {
46     int k;
47     char fn[1024];
48
49     /* Yes, I know, this is not portable. But who cares? It's
50      * for cosmetics only, anyway. */
51     
52     snprintf(fn, sizeof(fn), "/proc/%lu/exe", (unsigned long) getpid());
53
54     if ((k = readlink(fn, t, l-1)) < 0)
55         snprintf(t, l, "(unknown)");
56     else {
57         char *slash;
58
59         assert((size_t) k <= l-1);
60         t[k] = 0;
61
62         if ((slash = strrchr(t, '/')))
63             memmove(t, slash+1, strlen(slash)+1);
64     }
65 }
66
67 static void warning(const char *ident, const char *fmt, ...) {
68     va_list ap, ap2;
69     
70     assert(ident);
71     assert(fmt);
72
73     va_start(ap, fmt);
74     va_copy(ap2, ap);
75     
76     vfprintf(stderr, fmt, ap);
77     va_end(ap);
78
79     openlog(ident, LOG_PID, LOG_USER);
80     vsyslog(LOG_WARNING, fmt, ap2);
81     closelog();
82     va_end(ap2);
83 }
84
85 void avahi_warn_linkage(void) {
86     int w;
87     
88     pthread_mutex_lock(&linkage_mutex);
89     w = linkage_warning;
90     linkage_warning = 1;
91     pthread_mutex_unlock(&linkage_mutex);
92
93     if (!w && !getenv("AVAHI_COMPAT_NOWARN")) {
94         char exename[256];
95
96         get_exe_name(exename, sizeof(exename));
97
98         warning(exename, "*** WARNING: The application '%s' uses the "COMPAT_LAYER" compatiblity layer of Avahi. Please fix it to use the native API! ***\n", exename);
99     }
100 }
101
102 void avahi_warn_unsupported(const char *function) {
103     char exename[256];
104     get_exe_name(exename, sizeof(exename));
105
106     warning(exename, "*** WARNING: The application '%s' called '%s()' which is not supported (or only supported partially) in the "COMPAT_LAYER" compatiblity layer of Avahi. Please fix it to use the native API! ***\n", exename, function);
107 }
108
109
110