]> git.meshlink.io Git - catta/blob - avahi-compat-libdns_sd/warn.c
build avahi_exe_name() exclusively on Linux
[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 #ifndef CGI_SUBSYSTEM
43 #define CGI_SUBSYSTEM "libdns_sd"
44 #endif
45
46 static pthread_mutex_t linkage_mutex = PTHREAD_MUTEX_INITIALIZER;
47 static int linkage_warning = 0;
48
49 #ifdef __linux__
50
51 const char *avahi_exe_name(void) {
52     static char exe_name[1024] = "";
53     static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
54
55     /* Yes, I know, this is not portable. But who cares? It's for
56      * cosmetics only, anyway. */
57     
58     pthread_mutex_lock(&mutex);
59
60     if (exe_name[0] == 0) {
61         int k;
62         char fn[64];
63         
64         snprintf(fn, sizeof(fn), "/proc/%lu/exe", (unsigned long) getpid());
65         
66         if ((k = readlink(fn, exe_name, sizeof(exe_name)-1)) < 0)
67             snprintf(exe_name, sizeof(exe_name), "(unknown)");
68         else {
69             char *slash;
70             
71             assert((size_t) k <= sizeof(exe_name)-1);
72             exe_name[k] = 0;
73             
74             if ((slash = strrchr(exe_name, '/')))
75                 memmove(exe_name, slash+1, strlen(slash)+1);
76         }
77     }
78     
79     pthread_mutex_unlock(&mutex);
80
81     return exe_name;
82 }
83
84 #else
85
86 #ifdef __GNUC__
87 #warning "avahi_exe_name() needs to be implemented for your operating system"
88 #endif
89
90 const char *avahi_exe_name(void) {
91     return "(unknown)";
92 }
93
94 #endif
95
96 void avahi_warn(const char *fmt, ...) {
97     char msg[512]  = "*** WARNING *** ";
98     va_list ap;
99     size_t n;
100     
101     assert(fmt);
102                 
103     va_start(ap, fmt);
104     n = strlen(msg);
105     vsnprintf(msg + n, sizeof(msg) - n, fmt, ap);
106     va_end(ap);
107     
108     fprintf(stderr, "%s\n", msg);
109
110     openlog(avahi_exe_name(), LOG_PID, LOG_USER);
111     syslog(LOG_WARNING, "%s", msg);
112     closelog();
113 }
114
115 void avahi_warn_linkage(void) {
116     int w;
117     
118     pthread_mutex_lock(&linkage_mutex);
119     w = linkage_warning;
120     linkage_warning = 1;
121     pthread_mutex_unlock(&linkage_mutex);
122
123     if (!w && !getenv("AVAHI_COMPAT_NOWARN")) {
124         avahi_warn("The programme '%s' uses the "COMPAT_LAYER" compatiblity layer of Avahi.", avahi_exe_name());
125         avahi_warn("Please fix your application to use the native API of Avahi!");
126         avahi_warn("For more information see <http://0pointer.de/avahi-compat?s="CGI_SUBSYSTEM"&e=%s>", avahi_exe_name());
127     }
128 }
129
130 void avahi_warn_unsupported(const char *function) {
131     avahi_warn("The programme '%s' called '%s()' which is not supported (or only supported partially) in the "COMPAT_LAYER" compatiblity layer of Avahi.", avahi_exe_name(), function);
132     avahi_warn("Please fix your application to use the native API of Avahi!");
133     avahi_warn("For more information see <http://0pointer.de/avahi-compat?s="CGI_SUBSYSTEM"&e=%s&f=%s>", avahi_exe_name(), function);
134 }
135
136
137