~ubuntu-branches/ubuntu/vivid/slurm-llnl/vivid

« back to all changes in this revision

Viewing changes to src/common/plugin.c

  • Committer: Bazaar Package Importer
  • Author(s): Gennaro Oliva
  • Date: 2009-09-24 23:28:15 UTC
  • mfrom: (1.1.11 upstream) (3.2.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090924232815-enh65jn32q1ebg07
Tags: 2.0.5-1
* New upstream release 
* Changed dependecy from lib-mysqlclient15 to lib-mysqlclient 
* Added Default-Start for runlevel 2 and 4 and $remote_fs requirement in
  init.d scripts (Closes: #541252)
* Postinst checks for wrong runlevels 2 and 4 links
* Upgraded to standard version 3.8.3
* Add lintian overrides for missing slurm-llnl-configurator.html in doc
  base registration
* modified postrm scripts to ignore pkill return value in order to avoid
  postrm failure when no slurm process is running
* Checking for slurmctld.pid before cancelling running and pending
  jobs during package removal 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*****************************************************************************\
2
 
 * plugin.h - plugin architecture implementation.
 
2
 *  plugin.h - plugin architecture implementation.
3
3
 *****************************************************************************
4
4
 *  Copyright (C) 2002-2007 The Regents of the University of California.
5
 
 *  Copyright (C) 2008 Lawrence Livermore National Security.
 
5
 *  Copyright (C) 2008-2009 Lawrence Livermore National Security.
6
6
 *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
7
7
 *  Written by Jay Windley <jwindley@lnxi.com>.
8
 
 *  LLNL-CODE-402394.
 
8
 *  CODE-OCEC-09-009. All rights reserved.
9
9
 *  
10
10
 *  This file is part of SLURM, a resource management program.
11
 
 *  For details, see <http://www.llnl.gov/linux/slurm/>.
 
11
 *  For details, see <https://computing.llnl.gov/linux/slurm/>.
 
12
 *  Please also read the included file: DISCLAIMER.
12
13
 *  
13
14
 *  SLURM is free software; you can redistribute it and/or modify it under
14
15
 *  the terms of the GNU General Public License as published by the Free
43
44
#include <errno.h>
44
45
#include <sys/types.h>
45
46
#include <stdio.h>
46
 
#include <dlfcn.h>        /* don't know if there's an autoconf for this. */
 
47
#include <dlfcn.h>      /* don't know if there's an autoconf for this. */
47
48
#include <string.h>
48
49
 
49
50
#include "src/common/xmalloc.h"
79
80
        return rc;
80
81
}
81
82
 
 
83
const char * plugin_strerror(plugin_err_t e)
 
84
{
 
85
        switch (e) {
 
86
                case EPLUGIN_SUCCESS:
 
87
                        return ("Success");
 
88
                case EPLUGIN_NOTFOUND:
 
89
                        return ("Plugin file not found");
 
90
                case EPLUGIN_ACCESS_ERROR:
 
91
                        return ("Plugin access denied");
 
92
                case EPLUGIN_DLOPEN_FAILED:
 
93
                        return ("Dlopen of plugin file failed");
 
94
                case EPLUGIN_INIT_FAILED:
 
95
                        return ("Plugin init() callback failed");
 
96
                case EPLUGIN_MISSING_SYMBOL:
 
97
                        return ("Plugin name/type/version symbol missing");
 
98
        }
 
99
        return ("Unknown error");
 
100
}
 
101
 
82
102
int
83
103
plugin_peek( const char *fq_path,
84
104
                         char *plugin_type,
120
140
        return SLURM_SUCCESS;
121
141
}
122
142
 
123
 
plugin_handle_t
124
 
plugin_load_from_file( const char *fq_path )
 
143
plugin_err_t
 
144
plugin_load_from_file(plugin_handle_t *p, const char *fq_path)
125
145
{
126
 
        plugin_handle_t plug;
127
 
        int (*init)( void );
128
 
        
129
 
        /*
130
 
         * Try to open the shared object.  
 
146
        plugin_handle_t plug;
 
147
        int (*init)(void);
 
148
 
 
149
        *p = PLUGIN_INVALID_HANDLE;
 
150
 
 
151
        /*
 
152
         *  Check for file existence and access permissions
 
153
         */
 
154
        if (access(fq_path, R_OK) < 0) {
 
155
                if (errno == ENOENT)
 
156
                        return EPLUGIN_NOTFOUND;
 
157
                else
 
158
                        return EPLUGIN_ACCESS_ERROR;
 
159
        }
 
160
 
 
161
        /*
 
162
         * Try to open the shared object.
131
163
         *
132
164
         * Use RTLD_LAZY to allow plugins to use symbols that may be 
133
165
         * defined in only one slurm entity (e.g. srun and not slurmd),
135
167
         * entity from which it is available. (i.e. srun symbols are only
136
168
         * used in the context of srun, not slurmd.)
137
169
         *
138
 
         */
139
 
        plug = dlopen( fq_path, RTLD_LAZY );
140
 
        if ( plug == NULL ) {
141
 
                error( "plugin_load_from_file: dlopen(%s): %s",
142
 
                        fq_path,
143
 
                        _dlerror() );
144
 
                return PLUGIN_INVALID_HANDLE;
145
 
        }
146
 
        
147
 
        /* Now see if our required symbols are defined. */
148
 
        if ( ( dlsym( plug, PLUGIN_NAME ) == NULL ) ||
149
 
             ( dlsym( plug, PLUGIN_TYPE ) == NULL ) ||
150
 
             ( dlsym( plug, PLUGIN_VERSION ) == NULL ) ) {
151
 
                debug( "plugin_load_from_file: invalid symbol");
152
 
                /* slurm_seterrno( SLURM_PLUGIN_SYMBOLS ); */
153
 
                return PLUGIN_INVALID_HANDLE;
154
 
        }
155
 
 
156
 
        /*
157
 
         * Now call its init() function, if present.  If the function
158
 
         * returns nonzero, unload the plugin and signal an error.
159
 
         */
160
 
        if ( ( init = dlsym( plug, "init" ) ) != NULL ) {
161
 
                if ( (*init)() != 0 ) {
162
 
                        error( "plugin_load_from_file(%s): init() returned SLURM_ERROR", 
163
 
                                fq_path );
164
 
                        (void) dlclose( plug );
165
 
                        return PLUGIN_INVALID_HANDLE;
166
 
                }
167
 
        }
168
 
        
169
 
        return plug;
 
170
         */
 
171
        plug = dlopen(fq_path, RTLD_LAZY);
 
172
        if (plug == NULL) {
 
173
                error("plugin_load_from_file: dlopen(%s): %s",
 
174
                      fq_path,
 
175
                      _dlerror());
 
176
                return EPLUGIN_DLOPEN_FAILED;
 
177
        }
 
178
 
 
179
        /* Now see if our required symbols are defined. */
 
180
        if ((dlsym(plug, PLUGIN_NAME) == NULL) ||
 
181
            (dlsym(plug, PLUGIN_TYPE) == NULL) ||
 
182
            (dlsym(plug, PLUGIN_VERSION) == NULL)) {
 
183
                dlclose (plug);
 
184
                return EPLUGIN_MISSING_SYMBOL;
 
185
        }
 
186
 
 
187
        /*
 
188
         * Now call its init() function, if present.  If the function
 
189
         * returns nonzero, unload the plugin and signal an error.
 
190
         */
 
191
        if ((init = dlsym(plug, "init")) != NULL) {
 
192
                if ((*init)() != 0) {
 
193
                        dlclose(plug);
 
194
                        return EPLUGIN_INIT_FAILED;
 
195
                }
 
196
        }
 
197
 
 
198
        *p = plug;
 
199
        return EPLUGIN_SUCCESS;
170
200
}
171
201
 
172
202
plugin_handle_t
173
203
plugin_load_and_link(const char *type_name, int n_syms,
174
204
                    const char *names[], void *ptrs[])
175
205
{
176
 
        plugin_handle_t plug = PLUGIN_INVALID_HANDLE;
 
206
        plugin_handle_t plug = PLUGIN_INVALID_HANDLE;
177
207
        struct stat st;
178
208
        char *head=NULL, *dir_array=NULL, *so_name = NULL,
179
209
                *file_name=NULL;
210
240
                        debug4("No Good.");
211
241
                        xfree(file_name);
212
242
                } else {
213
 
                        plug = plugin_load_from_file(file_name);
 
243
                        plugin_load_from_file(&plug, file_name);
214
244
                        xfree(file_name);
215
245
                        if (plugin_get_syms(plug, n_syms, names, ptrs) >= 
216
246
                            n_syms) {
239
269
void
240
270
plugin_unload( plugin_handle_t plug )
241
271
{
242
 
        void (*fini)(void);
243
 
        
244
 
        if ( plug != PLUGIN_INVALID_HANDLE ) {
245
 
                if ( ( fini = dlsym( plug, "fini" ) ) != NULL ) {
246
 
                        (*fini)();
247
 
                }
248
 
                (void) dlclose( plug );
249
 
        }
 
272
        void (*fini)(void);
 
273
        
 
274
        if ( plug != PLUGIN_INVALID_HANDLE ) {
 
275
                if ( ( fini = dlsym( plug, "fini" ) ) != NULL ) {
 
276
                        (*fini)();
 
277
                }
 
278
                (void) dlclose( plug );
 
279
        }
250
280
}
251
281
 
252
282
 
253
283
void *
254
284
plugin_get_sym( plugin_handle_t plug, const char *name )
255
285
{
256
 
        if ( plug != PLUGIN_INVALID_HANDLE )
257
 
                return dlsym( plug, name );
258
 
        else
259
 
                return NULL;
 
286
        if ( plug != PLUGIN_INVALID_HANDLE )
 
287
                return dlsym( plug, name );
 
288
        else
 
289
                return NULL;
260
290
}
261
291
 
262
292
const char *
263
293
plugin_get_name( plugin_handle_t plug )
264
294
{
265
 
        if ( plug != PLUGIN_INVALID_HANDLE )
266
 
                return (const char *) dlsym( plug, PLUGIN_NAME );
267
 
        else
268
 
                return NULL;
 
295
        if ( plug != PLUGIN_INVALID_HANDLE )
 
296
                return (const char *) dlsym( plug, PLUGIN_NAME );
 
297
        else
 
298
                return NULL;
269
299
}
270
300
 
271
301
const char *
272
302
plugin_get_type( plugin_handle_t plug )
273
303
{
274
 
        if ( plug != PLUGIN_INVALID_HANDLE )
275
 
                return (const char *) dlsym( plug, PLUGIN_TYPE );
276
 
        else
277
 
                return NULL;
 
304
        if ( plug != PLUGIN_INVALID_HANDLE )
 
305
                return (const char *) dlsym( plug, PLUGIN_TYPE );
 
306
        else
 
307
                return NULL;
278
308
}
279
309
 
280
310
uint32_t
281
311
plugin_get_version( plugin_handle_t plug )
282
312
{
283
 
        uint32_t *ptr;
 
313
        uint32_t *ptr;
284
314
 
285
 
        if ( plug == PLUGIN_INVALID_HANDLE ) return 0;        
286
 
        ptr = (uint32_t *) dlsym( plug, PLUGIN_VERSION );
287
 
        return ptr ? *ptr : 0;
 
315
        if ( plug == PLUGIN_INVALID_HANDLE ) return 0;  
 
316
        ptr = (uint32_t *) dlsym( plug, PLUGIN_VERSION );
 
317
        return ptr ? *ptr : 0;
288
318
}
289
319
 
290
320
int
291
321
plugin_get_syms( plugin_handle_t plug,
292
 
                 int n_syms,
293
 
                 const char *names[],
294
 
                 void *ptrs[] )
 
322
                 int n_syms,
 
323
                 const char *names[],
 
324
                 void *ptrs[] )
295
325
{
296
 
        int i, count;
 
326
        int i, count;
297
327
 
298
 
        count = 0;
299
 
        for ( i = 0; i < n_syms; ++i ) {
300
 
                ptrs[ i ] = dlsym( plug, names[ i ] );
301
 
                if ( ptrs[ i ] ) 
 
328
        count = 0;
 
329
        for ( i = 0; i < n_syms; ++i ) {
 
330
                ptrs[ i ] = dlsym( plug, names[ i ] );
 
331
                if ( ptrs[ i ] ) 
302
332
                        ++count;
303
333
                else 
304
334
                        debug3("Couldn't find sym '%s' in the plugin",
305
335
                               names[ i ]);
306
336
        }
307
337
 
308
 
        return count;
 
338
        return count;
309
339
}
310
340