summaryrefslogtreecommitdiffstats
path: root/cpukit/libdl/rap.c
blob: c512571ac8a7ae3e9621168974aae7365e2bdd31 (plain) (blame)
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 *  COPYRIGHT (c) 2012 Chris Johns <chrisj@rtems.org>
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */
/**
 * @file
 *
 * @ingroup rtems_rap
 *
 * @brief RTEMS Application Loader
 *
 * This is the RAP implementation.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <pthread.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <rtems/libio_.h>

#include <dlfcn.h>
#include <rtems/rtl/rap.h>
#include <rtems/rtl/rtl.h>

#include "rtl-find-file.h"

/**
 * The global RAP data. This structure is allocated on the heap when the first
 * call to location an application and is never released.
 */
typedef struct rtems_rap_data_s
{
  pthread_once_t      once;
  rtems_mutex         lock;           /**< The RAP lock id */
  rtems_chain_control apps;           /**< List if loaded application. */
  int                 last_errno;     /**< Last error number. */
  char                last_error[64]; /**< Last error string. */
} rtems_rap_data_t;

/**
 * The RAP file data. This structure is allocated on the heap when a file is
 * loaded.
 */
typedef struct rtems_rap_app_s
{
  rtems_chain_node node;         /**< The node's link in the chain. */
  const char*      name;         /**< The file name */
  void*            handle;       /**< The dlopen handle. */
} rtems_rap_app_t;

/**
 * RTL entry.
 */
#if (RTL_GLUE(__USER_LABEL_PREFIX__, 1) == RTL_GLUE(_, 1))
  #define RTL_ENTRY_POINT "_rtems"
#else
  #define RTL_ENTRY_POINT "rtems"
#endif

/**
 * Static RAP data is returned to the user when the loader is locked.
 */
static rtems_rap_data_t rap_ = { .once = PTHREAD_ONCE_INIT };

/**
 * Verbose level for the RAP loader.
 */
static bool rap_verbose;

/**
 * RAP entry call signature.
 */
typedef int (*rtems_rap_entry_t)(int argc, const char* argv[]);

/**
 * Forward decl.
 */
static void rtems_rap_unlock (void);

static void
rtems_rap_data_init (void)
{
  /*
   * Create the RAP lock.
   */
  rtems_mutex_init (&rap_.lock, "RAP");

  /*
   * Initialise the objects list and create any required services.
   */
  rtems_chain_initialize_empty (&rap_.apps);
}

static rtems_rap_data_t*
rtems_rap_lock (void)
{
  pthread_once (&rap_.once, rtems_rap_data_init);
  rtems_mutex_lock (&rap_.lock);

  return &rap_;
}

static void
rtems_rap_unlock (void)
{
  rtems_mutex_unlock (&rap_.lock);
}

static rtems_rap_app_t*
rtems_rap_check_handle (void* handle)
{
  rtems_rap_app_t*  app;
  rtems_chain_node* node;

  app = handle;
  node = rtems_chain_first (&rap_.apps);

  while (!rtems_chain_is_tail (&rap_.apps, node))
  {
    rtems_rap_app_t* check = (rtems_rap_app_t*) node;
    if (check == app)
      return app;
    node = rtems_chain_next (node);
  }

  return NULL;
}

static rtems_rap_app_t*
rtems_rap_app_alloc (void)
{
  rtems_rap_app_t* app = malloc (sizeof (rtems_rap_app_t));
  memset (app, 0, sizeof (rtems_rap_app_t));
  rtems_chain_append (&rap_.apps, &app->node);
  return app;
}

static void
rtems_rap_app_free (rtems_rap_app_t* app)
{
  if (app->handle)
  {
    dlclose (app->handle);
    app->handle = NULL;
  }

  if (!rtems_chain_is_node_off_chain (&app->node))
    rtems_chain_extract (&app->node);
}

static bool
rtems_rap_match_name (rtems_rap_app_t* app, const char* name)
{
  const char* a;

  /*
   * Assume the app name is absolute, ie points to the file on disk. This means
   * there is at least one delimiter in the name.
   */

  if (strncmp (app->name, name, strlen (name)) == 0)
    return true;

  a = app->name + strlen (app->name) - 1;

  while (a >= app->name)
  {
    if (rtems_filesystem_is_delimiter (*a))
    {
      const char* n = name;

      ++a;

      while (*a && *n)
      {
        if (*a == '.')
        {
          if (*n == '\0')
            return true;
        }

        ++a;
        ++n;
      }

      return false;
    }

    --a;
  }

  return false;
}

static void
rtems_rap_get_rtl_error (void)
{
  rap_.last_errno =
    rtems_rtl_get_error (rap_.last_error, sizeof (rap_.last_error));
}

static void
rtems_rap_set_error (int error, const char* format, ...)
{
  rtems_rap_data_t* rap = rtems_rap_lock ();
  va_list           ap;
  va_start (ap, format);
  rap->last_errno = error;
  vsnprintf (rap->last_error, sizeof (rap->last_error), format, ap);
  rtems_rap_unlock ();
  va_end (ap);
}

bool
rtems_rap_load (const char* name, int mode, int argc, const char* argv[])
{
  rtems_rap_data_t* rap = rtems_rap_lock ();

  if (!rap)
    return false;

  if (rap_verbose)
    printf ("rap: loading '%s'\n", name);

  /*
   * See if the app has already been loaded.
   */
  if (!rtems_rap_find (name))
  {
    rtems_rap_app_t*  app;
    rtems_rap_entry_t init;
    rtems_rap_entry_t fini;
    size_t            size = 0;
    int               r;

    /*
     * Allocate a new application descriptor and attempt to load it.
     */
    app = rtems_rap_app_alloc ();
    if (app == NULL)
    {
      rtems_rap_set_error (ENOMEM, "no memory for application");
      rtems_rap_unlock ();
      return false;
    }

    /*
     * Find the file in the file system using the search path.
     */
    if (!rtems_rtl_find_file (name, getenv ("PATH"), &app->name, &size))
    {
      rtems_rap_set_error (ENOENT, "file not found");
      rtems_rap_app_free (app);
      rtems_rap_unlock ();
      return false;
    }

    app->handle = dlopen (app->name, RTLD_NOW | mode);
    if (!app->handle)
    {
      rtems_rap_get_rtl_error ();
      rtems_rap_app_free (app);
      rtems_rap_unlock ();
      return false;
    }

    init = dlsym (app->handle, RTL_ENTRY_POINT);
    if (!init)
    {
      rtems_rap_get_rtl_error ();
      rtems_rap_app_free (app);
      rtems_rap_unlock ();
      return false;
    }

    fini = dlsym (app->handle, RTL_ENTRY_POINT);
    if (!fini)
    {
      rtems_rap_get_rtl_error ();
      rtems_rap_app_free (app);
      rtems_rap_unlock ();
      return false;
    }

    r = init (argc, argv);
    if (r != 0)
    {
      rtems_rap_set_error (r, "init call failure");
      rtems_rap_app_free (app);
      rtems_rap_unlock ();
      return false;
    }
  }

  rtems_rap_unlock ();

  return true;
}

bool
rtems_rap_unload (const char* name)
{
  rtems_rap_app_t*  app;
  rtems_rap_entry_t fini;
  int               r;

  rtems_rap_lock ();

  app = rtems_rap_find (name);

  if (rap_verbose)
    printf ("rap: unloading '%s'\n", name);

  if (!app)
  {
    rtems_rap_set_error (ENOENT, "invalid handle");
    rtems_rap_unlock ();
    return false;
  }

  fini = dlsym (app->handle, RTL_ENTRY_POINT);
  if (!fini)
  {
    rtems_rap_get_rtl_error ();
    rtems_rap_unlock ();
    return false;
  }

  r = fini (0, NULL);
  if (r != 0)
  {
    rtems_rap_set_error (r, "fini failure");
    rtems_rap_unlock ();
    return false;
  }

  rtems_rap_app_free (app);
  rtems_rap_unlock ();

  return true;
}

void*
rtems_rap_find (const char* name)
{
  rtems_rap_data_t* rap = rtems_rap_lock ();
  rtems_chain_node* node;

  node = rtems_chain_first (&rap->apps);

  while (!rtems_chain_is_tail (&rap->apps, node))
  {
    rtems_rap_app_t* app = (rtems_rap_app_t*) node;
    if (rtems_rap_match_name (app, name))
    {
      rtems_rap_unlock ();
      return app;
    }
    node = rtems_chain_next (node);
  }

  rtems_rap_unlock ();

  return NULL;
}

bool
rtems_rap_iterate (rtems_rap_iterator_t iterator)
{
  rtems_rap_data_t* rap = rtems_rap_lock ();
  rtems_chain_node* node;
  bool              result = true;

  node = rtems_chain_first (&rap->apps);

  while (!rtems_chain_is_tail (&rap->apps, node))
  {
    rtems_rap_app_t* app = (rtems_rap_app_t*) node;
    result = iterator (app);
    if (!result)
      break;
    node = rtems_chain_next (node);
  }

  rtems_rap_unlock ();

  return result;
}

const char*
rtems_rap_name (void* handle)
{
  rtems_rap_app_t* app = rtems_rap_check_handle (handle);
  if (app)
    return app->name;
  return NULL;
}

void*
rtems_rap_dl_handle (void* handle)
{
  rtems_rap_app_t* app = rtems_rap_check_handle (handle);
  if (app)
    return app->handle;
  return NULL;
}

int
rtems_rap_get_error (char* message, size_t max_message)
{
  rtems_rap_data_t* rap = rtems_rap_lock ();
  int               last_errno = rap->last_errno;
  strncpy (message, rap->last_error, sizeof (rap->last_error));
  rtems_rap_unlock ();
  return last_errno;
}