~ubuntu-branches/debian/jessie/gamin/jessie

« back to all changes in this revision

Viewing changes to lib/gam_error.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck
  • Date: 2007-03-23 14:43:49 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070323144349-1inpdk22uaneks9h
Tags: 0.1.8-2
* debian/control: Improve long description. (Closes: #405347)
* debian/patches/14_nfs-fix.patch: Fix gam_server startup for Thunar.
  Thanks to Maximiliano Curia. (Closes: #403247)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * gam_error.c: the debugging intrastructure
 
3
 *
 
4
 * Allow to use GAM_DEBUG environment variable or SIG_USR2 for
 
5
 * dynamic debugging of clients and server.
 
6
 *
 
7
 * Daniel Veillard <veillard@redhat.com>
 
8
 * See the Copyright file.
 
9
 */
1
10
#include <config.h>
2
11
#include <stdio.h>
3
12
#include <stdarg.h>
4
13
#include <stdlib.h>
5
14
#include <signal.h>
 
15
#include <errno.h>
6
16
#include "gam_error.h"
7
17
 
8
 
typedef void (*signal_handler)(int);
9
 
 
 
18
typedef void (*signal_handler) (int);
 
19
 
 
20
extern void gam_show_debug(void);
 
21
extern void gam_got_signal (void);
 
22
 
 
23
int gam_debug_active = 0;
10
24
static int initialized = 0;
11
25
static int do_debug = 0;
12
26
static int got_signal = 0;
13
27
static FILE *debug_out = NULL;
14
28
 
15
29
static void
16
 
gam_error_handle_signal(void) {
 
30
gam_error_handle_signal(void)
 
31
{
17
32
    if (got_signal == 0)
18
33
        return;
19
34
 
 
35
    got_signal = 0;
 
36
 
20
37
    if (do_debug == 0) {
21
 
       char path[50] = "/tmp/gamin_debug_XXXXXX";
22
 
       int fd = mkstemp(path);
23
 
       if (fd >= 0) {
24
 
           debug_out = fdopen(fd, "a");
25
 
           if (debug_out != NULL) 
26
 
               do_debug = 1;
27
 
       }
 
38
        if (debug_out != stderr) {
 
39
            char path[50] = "/tmp/gamin_debug_XXXXXX";
 
40
            int fd = mkstemp(path);
 
41
 
 
42
            if (fd >= 0) {
 
43
                debug_out = fdopen(fd, "a");
 
44
                if (debug_out != NULL) {
 
45
                    do_debug = 1;
 
46
                    gam_debug_active = 1;
 
47
                    gam_show_debug();
 
48
                }
 
49
            }
 
50
        }
28
51
    } else {
29
 
       do_debug = 0;
30
 
       if (debug_out != NULL) {
31
 
           fclose(debug_out);
32
 
           debug_out = NULL;
33
 
       }
 
52
        if (debug_out != stderr) {
 
53
            do_debug = 0;
 
54
            gam_debug_active = 0;
 
55
            if (debug_out != NULL) {
 
56
                fflush(debug_out);
 
57
                fclose(debug_out);
 
58
                debug_out = NULL;
 
59
            }
 
60
        }
34
61
    }
35
 
    got_signal = 0;
36
62
}
37
63
 
38
 
static void 
39
 
gam_error_signal(int no) {
 
64
 
 
65
static void
 
66
gam_error_signal(int no)
 
67
{
40
68
    got_signal = !got_signal;
 
69
    gam_debug_active = -1;      /* force going into gam_debug() */
 
70
    gam_got_signal ();
41
71
}
42
72
 
 
73
/**
 
74
 * gam_error_init:
 
75
 *
 
76
 * Initialization routine for the error and debug handling.
 
77
 */
43
78
void
44
 
gam_error_init(void) {
45
 
 
 
79
gam_error_init(void)
 
80
{
46
81
    if (initialized == 0) {
47
 
        signal_handler prev;
 
82
        struct sigaction oldact;
48
83
 
49
84
        initialized = 1;
50
 
        if (getenv("GAM_DEBUG") != NULL)
51
 
            do_debug = 1;
52
 
        prev = signal(SIGUSR2, gam_error_signal);
53
 
        /* if there is already an handler switch back to the original
54
 
           to avoid disturbing the application behaviour */
55
 
        if ((prev != SIG_IGN) && (prev != SIG_DFL) && (prev != NULL))
56
 
            signal(SIGUSR2, prev);
 
85
 
 
86
        if (getenv("GAM_DEBUG") != NULL) {
 
87
            debug_out = stderr;
 
88
            gam_debug_active = 1;
 
89
            do_debug = 1;
 
90
            /* Fake the signal */
 
91
            got_signal = 1;
 
92
            gam_error_handle_signal();
 
93
        }
 
94
 
 
95
        /* if there is already an handler, leave it as is to
 
96
         * avoid disturbing the application's behaviour */
 
97
        if (sigaction (SIGUSR2, NULL, &oldact) == 0) {
 
98
            if (oldact.sa_handler == NULL && oldact.sa_sigaction == NULL)
 
99
                signal(SIGUSR2, gam_error_signal);
 
100
        }
57
101
    }
58
102
}
59
103
 
60
104
/**
 
105
 * gam_error_init:
 
106
 *
 
107
 * Checking routine to call from time to time to handle asynchronous
 
108
 * error debugging events.
 
109
 */
 
110
void
 
111
gam_error_check(void)
 
112
{
 
113
    if (initialized == 0)
 
114
        gam_error_init();
 
115
 
 
116
    if (got_signal)
 
117
        gam_error_handle_signal();
 
118
}
 
119
 
 
120
int
 
121
gam_errno(void)
 
122
{
 
123
    return (errno);
 
124
}
 
125
 
 
126
/**
61
127
 * gam_error:
62
128
 * @file: the filename where the error was detected
63
129
 * @line: the line where the error was detected
81
147
 
82
148
    if ((file == NULL) || (function == NULL) || (format == NULL))
83
149
        return;
 
150
 
84
151
    va_start(args, format);
85
152
    vfprintf((debug_out ? debug_out : stderr), format, args);
86
153
    va_end(args);
 
154
 
87
155
    if (debug_out)
88
 
        fflush(debug_out);
 
156
        fflush(debug_out);
89
157
}
90
158
 
91
159
/**
110
178
    if (got_signal)
111
179
        gam_error_handle_signal();
112
180
 
113
 
    if (do_debug == 0)
 
181
    if ((do_debug == 0) || (gam_debug_active == 0))
114
182
        return;
 
183
 
115
184
    if ((file == NULL) || (function == NULL) || (format == NULL))
116
185
        return;
 
186
 
117
187
    va_start(args, format);
118
188
    vfprintf((debug_out ? debug_out : stdout), format, args);
119
189
    va_end(args);
120
190
    if (debug_out)
121
 
        fflush(debug_out);
 
191
        fflush(debug_out);
122
192
}