File: init.c

package info (click to toggle)
globus-net-manager 1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,988 kB
  • sloc: sh: 11,139; ansic: 7,298; perl: 200; python: 181; makefile: 178
file content (261 lines) | stat: -rw-r--r-- 8,210 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
/*
 * Copyright 1999-2014 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.
 */

/**
 * @file
 * @brief globus_net_manager_context_init()
 */

#include "globus_net_manager_context.h"


static 
globus_result_t
globus_l_net_manager_context_load_entry(
    const char *                            name,
    globus_i_net_manager_context_entry_t ** entry)
{
    globus_extension_handle_t               ext_handle;
    globus_net_manager_t *                  loaded_manager;
    char *                                  dll_name = NULL;
    globus_i_net_manager_context_entry_t *  ent = NULL;
    int                                     rc;
    globus_result_t                         result = GLOBUS_SUCCESS;

    /* is module already in registry? */
    loaded_manager = (globus_net_manager_t *) globus_extension_lookup(
        &ext_handle, GLOBUS_NET_MANAGER_REGISTRY, (void *) name);
    if(loaded_manager == NULL)
    {
        /* load and activate the dll */
        dll_name = globus_common_create_string(
            "globus_net_manager_%s", name);
        if (dll_name == NULL)
        {
            result = GlobusNetManagerErrorMemory("dll_name");
            goto dll_name_alloc;
        }

        rc = globus_extension_activate(dll_name);        
        if(rc != GLOBUS_SUCCESS)
        {
            result = GlobusNetManagerErrorManager(
                rc, name, "attempting to activate module.");
            goto error_activate;
        }
    
        /* now module should be in registry */
        loaded_manager = globus_extension_lookup(
            &ext_handle, GLOBUS_NET_MANAGER_REGISTRY, (void *) name);
        if(loaded_manager == NULL)
        {
            result = GlobusNetManagerErrorManager(
                rc, name, "attempting to load activated module.");
            goto error_lookup;
        }
    }
    ent = malloc(sizeof(globus_i_net_manager_context_entry_t));
    if (ent == NULL)
    {
        result = GlobusNetManagerErrorMemory("ent");
        goto error_ent;
    }
    *ent = (globus_i_net_manager_context_entry_t) {
        .manager = loaded_manager,
        .ext_handle = ext_handle,
        .name = strdup(name),
        .dll_name = dll_name
    };

    if (ent->name == NULL)
    {
        result = GlobusNetManagerErrorMemory("name");
    
        free(ent);
        ent = NULL;
error_ent:
error_lookup:
        if (dll_name)
        {
            globus_extension_deactivate(dll_name);
error_activate:
            free(dll_name);
        }
dll_name_alloc:
        if (result == GLOBUS_SUCCESS)
        {
            result = GLOBUS_FAILURE;
        }
    }
    *entry = ent;
    return result;
}
/* globus_l_net_manager_context_load_entry() */


/**
 * @brief Initialize Context
 * @ingroup globus_net_manager_context
 * @details
 * This functions initializes *context* with the attribute list *attrs*.
 * 
 * @param [out] context
 *     A pointer to the context to initialize.
 * @param [in] attrs
 *     An array of attributes to initialize the context with.
 * @return
 *     On error, the 'context' is set to NULL and this function returns
 *     an error object. Otherwise this function returns 'GLOBUS_SUCCESS'
 */
globus_result_t
globus_net_manager_context_init(
    globus_net_manager_context_t *      context,
    const globus_net_manager_attr_t *   attrs)
{
    globus_i_net_manager_context_t *    ctx;
    globus_result_t                     result;
    int                                 i;
    int                                 j;
    int                                 rc;
    int                                 max_attr_count;
    int                                 attrnum;
    char *                              current_scope = NULL;
    globus_i_net_manager_context_entry_t *  ent = NULL;

    if(context == NULL || attrs == NULL || attrs[0].scope == NULL)
    {
        result = GlobusNetManagerErrorParameter("No parameter may be NULL.");
        goto error_no_attr;
    }
    
    ctx = malloc(sizeof(globus_i_net_manager_context_t));
    if(ctx == NULL)
    {
        result = GlobusNetManagerErrorMemory("context");
        goto error_ctx_mem;
    }
    ctx->managers = NULL;

    for(max_attr_count = 0; 
        attrs[max_attr_count].scope != NULL;
        max_attr_count++);
    
    for(i = 0; attrs[i].scope != NULL; i++)
    {
        if(strcmp(attrs[i].scope, "global") != 0)
        {
            /* Ignore global scope attributes here. They get added
             * to each new manager's attribute list when we encounter them.
             */
            if (current_scope == NULL ||
                     strcmp(attrs[i].scope, current_scope) != 0)
            {
                /* start of a new manager entry, either explicitly or by
                 * changing scope
                 */
                if(strcmp(attrs[i].scope, "net_manager") == 0 && 
                    strcmp(attrs[i].name, "manager") == 0)
                {
                    ent = NULL;
                    attrnum = 0;
                    current_scope = attrs[i].value;
                }
                else
                {
                    ent = NULL;
                    attrnum = 0;
                    current_scope = attrs[i].scope;
                }

                result = globus_l_net_manager_context_load_entry(
                    current_scope, &ent);
                if(result)
                {
                    goto error_load;
                }

                ent->attrs = calloc(
                    max_attr_count, sizeof(globus_net_manager_attr_t));
                for(j = 0; attrs[j].scope != NULL; j++)
                {
                    if(strcmp(attrs[j].scope, "global") == 0)
                    {
                        result = globus_net_manager_attr_init(
                                &ent->attrs[attrnum++],
                                attrs[j].scope,
                                attrs[j].name,
                                attrs[j].value);
                        if(result)
                        {
                            goto error_global_attr;
                        }
                    }
                }
                ent->attrs[attrnum] = globus_net_manager_null_attr;
                
                rc = globus_list_insert(&ctx->managers, ent);
                if (rc != GLOBUS_SUCCESS)
                {
                    result = GlobusNetManagerErrorMemory("managers");
                    goto error_list_insert;
                }
            }
            /* attrs for the current manager entry */
            if(current_scope && strcmp(attrs[i].scope, current_scope) == 0)
            {
                result = globus_net_manager_attr_init(
                        &ent->attrs[attrnum++],
                        attrs[i].scope,
                        attrs[i].name,
                        attrs[i].value);
                if(result)
                {
                    goto error_attr;
                }
                ent->attrs[attrnum] = globus_net_manager_null_attr;
            }
        }
    }
    
    *context = ctx;
    return GLOBUS_SUCCESS;

error_global_attr:
error_list_insert:
    globus_extension_release(ent->ext_handle);
    /* if dll_name is set, driver was activated by me */
    if(ent->dll_name)
    {
        globus_extension_deactivate(ent->dll_name);
        free(ent->dll_name);
    }
    globus_net_manager_attr_array_delete(ent->attrs);
    free(ent->name);
    free(ent);

error_attr:
error_load:
    globus_net_manager_context_destroy(ctx);
error_ctx_mem:
error_no_attr:
    if (context)
    {
        *context = NULL;
    }

    return result;
}
/* globus_net_manager_context_init() */