File: globus_error_errno.c

package info (click to toggle)
globus-common 14.7-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,308 kB
  • sloc: ansic: 33,976; sh: 9,902; makefile: 532; perl: 342; xml: 322
file content (344 lines) | stat: -rw-r--r-- 9,492 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * Copyright 1999-2006 University of Chicago
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef GLOBUS_DONT_DOCUMENT_INTERNAL
/**
 * @file globus_error_errno.c
 *
 * $RCSfile: globus_error_errno.c,v $
 * $Revision: 1.10 $
 * $Date: 2006/01/19 05:54:14 $
 */
#endif

#include "globus_error_errno.h"
#include "globus_i_error_generic.h"
#include "globus_object.h"
#include "globus_module.h"
#include "globus_error.h"
#include "globus_error_generic.h"
#include "globus_libc.h"
#include <string.h>

/**
 * @name Construct Error
 */
/*@{*/
/**
 * Allocate and initialize an error of type GLOBUS_ERROR_TYPE_ERRNO
 * @ingroup globus_errno_error_object
 *
 * @param base_source
 *        Pointer to the originating module.
 * @param base_cause
 *        The error object causing the error. If this is the original
 *        error, this paramater may be NULL.
 * @param system_errno
 *        The system errno.
 * @return
 *        The resulting error object. It is the user's responsibility
 *        to eventually free this object using globus_object_free(). A
 *        globus_result_t may be obtained by calling
 *        globus_error_put() on this object.        
 */
globus_object_t *
globus_error_construct_errno_error(
    globus_module_descriptor_t *        base_source,
    globus_object_t *                   base_cause,
    const int                           system_errno)
{
    globus_object_t *                   error;
    globus_object_t *                   newerror;

    newerror = globus_object_construct(GLOBUS_ERROR_TYPE_ERRNO);
    error = globus_error_initialize_errno_error(
        newerror,
        base_source,
        base_cause,
        system_errno);

    if (error == NULL)
    {
        globus_object_free(newerror);
    }

    return error;
}/* globus_error_construct_errno_error() */
/*@}*/

/**
 * @name Initialize Error
 */
/*@{*/
/**
 * Initialize a previously allocated error of type
 * GLOBUS_ERROR_TYPE_ERRNO
 * @ingroup globus_errno_error_object 
 *
 * @param error
 *        The previously allocated error object.
 * @param base_source
 *        Pointer to the originating module.
 * @param base_cause
 *        The error object causing the error. If this is the original
 *        error this paramater may be NULL.
 * @param system_errno
 *        The system errno.
 * @return
 *        The resulting error object. You may have to call
 *        globus_error_put() on this object before passing it on.
 */
globus_object_t *
globus_error_initialize_errno_error(
    globus_object_t *                   error,
    globus_module_descriptor_t *        base_source,
    globus_object_t *                   base_cause,
    const int                           system_errno)
{
    int *                               instance_data;

    instance_data = (int *) globus_malloc(sizeof(int));

    *instance_data = system_errno;

    globus_object_set_local_instance_data(error, (void *) instance_data);

    return globus_error_initialize_base(error,
                                        base_source,
                                        base_cause);
}/* globus_error_initialize_errno_error() */
/*@}*/

/**
 * @name Get Errno
 */
/*@{*/
/**
 * Retrieve the system errno from a errno error object.
 * @ingroup globus_errno_error_accessor  
 *
 * @param error
 *        The error from which to retrieve the errno
 * @return
 *        The errno stored in the object
 */
int
globus_error_errno_get_errno(
    globus_object_t *                   error)
{
    return *((int *) globus_object_get_local_instance_data(error));
}/* globus_error_errno_get_errno */
/*@}*/

/**
 * @name Set Errno
 */
/*@{*/
/**
 * Set the errno in a errno error object.
 * @ingroup globus_errno_error_accessor  
 *
 * @param error
 *        The error object for which to set the errno
 * @param system_errno
 *        The system errno
 * @return
 *        void
 */
void
globus_error_errno_set_errno(
    globus_object_t *                   error,
    const int                           system_errno)
{
    *((int *) globus_object_get_local_instance_data(error)) = system_errno;
}/* globus_error_errno_set_errno */
/*@}*/

/**
 * @name Error Match
 */
/*@{*/
/**
 * Check whether the error originated from a specific module and
 * matches a specific errno.
 * @ingroup globus_errno_error_utility  
 *
 * This function checks whether the error or any of it's causative
 * errors originated from a specific module and contains a specific
 * errno. If the module descriptor is left unspecified this function
 * will check for any error of the specified errno and vice versa.
 *
 * @param error
 *        The error object for which to perform the check
 * @param module
 *        The module descriptor to check for
 * @param system_errno
 *        The errno to check for
 * @return
 *        GLOBUS_TRUE - the error matched the module and errno
 *        GLOBUS_FALSE - the error failed to match the module and errno
 */
globus_bool_t
globus_error_errno_match(
    globus_object_t *                   error,
    globus_module_descriptor_t *        module,
    int                                 system_errno)
{
    globus_module_descriptor_t *        source_module;
    int                                 current_errno;
    
    if(error == NULL)
    {
        return GLOBUS_FALSE;
    }

    if(globus_object_get_type(error) != GLOBUS_ERROR_TYPE_ERRNO)
    {
        /* not our type, skip it */
        return globus_error_errno_match(
            globus_error_get_cause(error),
            module,
            system_errno);
    }

    source_module = globus_error_get_source(error);
    current_errno = globus_error_errno_get_errno(error);
    
    if(source_module == module && current_errno == system_errno)
    {
        return GLOBUS_TRUE;
    }
    else
    {
        return globus_error_errno_match(
            globus_error_get_cause(error),
            module,
            system_errno);
    }
}/* globus_error_errno_match */
/*@}*/


/**
 * @name Wrap Errno Error
 */
/*@{*/
/**
 * Allocate and initialize an error of type GLOBUS_ERROR_TYPE_GLOBUS
 * which contains a causal error of type GLOBUS_ERROR_TYPE_ERRNO.
 * @ingroup globus_errno_error_utility  
 *
 * @param base_source
 *        Pointer to the originating module.
 * @param system_errno
 *        The errno to use when generating the causal error.
 * @param type
 *        The error type. We may reserve part of this namespace for
 *        common errors. Errors not in this space are assumed to be
 *        local to the originating module.
 * @param source_file
 *        Name of file.  Use __FILE__
 * @param source_func
 *        Name of function.  Use _globus_func_name and declare your func with
 *        GlobusFuncName(<name>)
 * @param source_line
 *        Line number.  Use __LINE__
 * @param short_desc_format
 *        Short format string giving a succinct description
 *        of the error. To be passed on to the user.
 * @param ...
 *        Arguments for the format string.
 * @return
 *        The resulting error object. It is the user's responsibility
 *        to eventually free this object using globus_object_free(). A
 *        globus_result_t may be obtained by calling
 *        globus_error_put() on this object.        
 */
globus_object_t *
globus_error_wrap_errno_error(
    globus_module_descriptor_t *        base_source,
    int                                 system_errno,
    int                                 type,
    const char *                        source_file,
    const char *                        source_func,
    int                                 source_line,
    const char *                        short_desc_format,
    ...)
{
    globus_object_t *                   causal_error;
    globus_object_t *                   error;
    va_list                             ap;
    char *                              fmt = GLOBUS_NULL;
    char *                              sys_error;

    causal_error = globus_error_construct_errno_error(
        base_source,
        NULL,
        system_errno);

    if(!causal_error)
    {
        return GLOBUS_NULL;
    }
    
    va_start(ap, short_desc_format);
    
    sys_error = strerror(system_errno);
    if(sys_error)
    {
        fmt = (char *) globus_malloc(                       /* ': \0' */
            strlen(short_desc_format) + strlen(sys_error) + 3);
        if(fmt)
        {
            sprintf(fmt, "%s: %s", short_desc_format, sys_error);
        }
    }
    
    if(!fmt)
    {
        fmt = (char *) short_desc_format;
    }
    
    error = globus_error_v_construct_error(
        base_source,
        causal_error,
        type,
        source_file,
        source_func,
        source_line,
        fmt,
        ap);

    va_end(ap);
    
    if(fmt != short_desc_format)
    {
        globus_free(fmt);
    }
    
    if(error == GLOBUS_NULL)
    {
        globus_object_free(causal_error);
    }
    
    return error;
    
}/* globus_error_wrap_errno_error */
/*@}*/