summaryrefslogtreecommitdiff
path: root/console-client/driver.c
blob: 5c4630a0f8507fcea3080d690a4dcd393b98fb12 (plain)
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
/* driver.c - The console client driver code.
   Copyright (C) 2002 Free Software Foundation, Inc.
   Written by Marcus Brinkmann.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <dlfcn.h>

#include <cthreads.h>

#include "driver.h"


/* The number of entries by which we grow a driver or component list
   if we need more space.  */
#define LIST_GROW 4

/* The path where we search for drivers, in addition to the default
   path.  The directories are separated by '\0' and terminated with an
   empty string.  XXX Should use argz or something.  XXX Should get a
   protective lock.  */
char *driver_path;

/* The driver list lock, the list itself, its current length and the
   total number of entries in the list.  */
struct mutex driver_list_lock;
driver_t driver_list;
size_t driver_list_len;
size_t driver_list_alloc;


/* Initialize the driver framework.  */
error_t
driver_init (void)
{
  mutex_init (&driver_list_lock);
  mutex_init (&display_list_lock);
  mutex_init (&input_list_lock);
  mutex_init (&bell_list_lock);
  return 0;
}


/* Deinitialize and unload all loaded drivers and deinitialize the
   driver framework.  */
error_t
driver_fini (void)
{
  unsigned int i;

  mutex_lock (&driver_list_lock);
  for (i = 0; i < driver_list_len; i++)
    {
      driver_list[i].ops->fini (driver_list[i].handle, 1);
      dlclose (driver_list[i].module);
      free (driver_list[i].name);
      free (driver_list[i].driver);
    }
  driver_list_len = 0;
  mutex_unlock (&driver_list_lock);
  return 0;
}


/* Load, intialize and (if START is non-zero) start the driver DRIVER
   under the given NAME (which must be unique among all loaded
   drivers) with arguments ARGZ with length ARGZ_LEN.  This funtion
   will grab the driver list lock.  The driver itself might try to
   grab the display, input source and bell list locks as well.  */
error_t driver_add (const char *const name, const char *const driver,
		    int argc, char *argv[], int *next, int start)
{
  error_t err;
  static char cons_defpath[] = CONSOLE_DEFPATH;
  driver_ops_t ops;
  char *filename = NULL;
  char *modname;
  void *shobj = NULL;
  driver_t drv;
  unsigned int i;
  char *dir = driver_path;
  int defpath = 0;

  mutex_lock (&driver_list_lock);
  for (i = 0; i < driver_list_len; i++)
    if (driver_list[i].name && !strcmp (driver_list[i].name, name))
      {
	mutex_unlock (&driver_list_lock);
	return EEXIST;
      }

  if (!dir || !*dir)
    {
      dir = cons_defpath;
      defpath = 1;
    }

  while (dir)
    {
      if (filename)
	free (filename);
      if (asprintf (&filename,
		    "%s/%s%s", dir, driver, CONSOLE_SONAME_SUFFIX) < 0)
	{
	  mutex_unlock (&driver_list_lock);
	  return ENOMEM;
	}

      errno = 0;
      shobj = dlopen (filename, RTLD_LAZY);
      if (!shobj)
	{
	  const char *errstring = dlerror (); /* Must always call or it leaks! */
	  if (errno != ENOENT)
	    {
	      free (filename);
	      mutex_unlock (&driver_list_lock);
	      return errno ?: EGRATUITOUS;
	    }
	}
      else
	break;

      dir += strlen (dir) + 1;
      if (!*dir)
	{
	  if (defpath)
	    break;
	  else
	    {
	      dir = cons_defpath;
	      defpath = 1;
	    }
	}
    }

  if (!shobj)
    {
      if (filename)
	free (filename);
      mutex_unlock (&driver_list_lock);
      return ENOENT;
    }

  if (asprintf (&modname, "driver_%s_ops", driver) < 0)
    {
      dlclose (shobj);
      free (filename);
      mutex_unlock (&driver_list_lock);
      return ENOMEM;
    }

  ops = dlsym (shobj, modname);
  free (modname);
  if (!ops || !ops->init)
    {
      dlclose (shobj);
      free (filename);
      mutex_unlock (&driver_list_lock);
      return EGRATUITOUS;
    }

  if (driver_list_len == driver_list_alloc)
    {
      size_t new_alloc = driver_list_alloc + LIST_GROW;
      driver_t new_list = realloc (driver_list,
				   new_alloc * sizeof (*driver_list));
      if (!new_list)
	{
	  dlclose (shobj);
	  free (filename);
	  mutex_unlock (&driver_list_lock);
	  return errno;
	}
      driver_list = new_list;
      driver_list_alloc = new_alloc;
    }
  drv = &driver_list[driver_list_len];

  drv->name = strdup (name);
  drv->driver = strdup (driver);
  drv->filename = filename;
  drv->ops = ops;
  drv->module = shobj;
  if (!drv->name || !drv->driver)
    {
      if (drv->name)
	free (drv->name);
      if (drv->driver)
	free (drv->driver);
      dlclose (shobj);
      free (filename);
      mutex_unlock (&driver_list_lock);
      return ENOMEM;
    }

  /* If we will start the driver, the init function must not exit.  */
  err = (*drv->ops->init) (&drv->handle, start, argc, argv, next);
  if (!err && start && drv->ops->start)
    err = (*drv->ops->start) (drv->handle);
  if (err)
    {
      free (drv->name);
      free (drv->driver);
      dlclose (shobj);
      free (filename);
      mutex_unlock (&driver_list_lock);
      return err;
    }

  driver_list_len++;
  mutex_unlock (&driver_list_lock);
  return 0;
}


/* Start all drivers.  Only used once at start up, after all the
   option parsing and driver initialization.

   Returns 0 on success, and the name of a driver if it initializing
   that driver fails.  */
error_t
driver_start (char **name)
{
  error_t err = 0;
  unsigned int i;

  mutex_lock (&driver_list_lock);
  for (i = 0; i < driver_list_len; i++)
    {
      if (driver_list[i].ops->start)
	err = (*driver_list[i].ops->start) (driver_list[i].handle);
      if (err)
	{
	  *name = driver_list[i].name;
	  while (--i >= 0)
	    (*driver_list[i].ops->fini) (driver_list[i].handle, 1);
	  break;
	}
    }
  mutex_unlock (&driver_list_lock);
  return err;
}


/* Deinitialize and unload the driver with the name NAME.  This
   function will grab the driver list lock.  The driver might try to
   grab the display, input source and bell list locks as well.  */
error_t driver_remove (const char *const name)
{
  error_t err;
  unsigned int i;

  mutex_lock (&driver_list_lock);
  for (i = 0; i < driver_list_len; i++)
    if (driver_list[i].name && !strcmp (driver_list[i].name, name))
      {
	err = driver_list[i].ops->fini (driver_list[i].handle, 0);
	if (!err)
	  {
	    dlclose (driver_list[i].module);
	    free (driver_list[i].name);
	    free (driver_list[i].driver);
	    free (driver_list[i].filename);
	    while (i + 1 < driver_list_len)
	      {
		driver_list[i] = driver_list[i + 1];
		i++;
	      }
	    driver_list_len--;
	  }
	mutex_unlock (&driver_list_lock);
	return err;
      }
  mutex_unlock (&driver_list_lock);
  return ESRCH;
}


#define ADD_REMOVE_COMPONENT(component)					\
struct mutex component##_list_lock;					\
component##_t component##_list;						\
size_t component##_list_len;						\
size_t component##_list_alloc;						\
									\
error_t									\
driver_add_##component (component##_ops_t ops, void *handle)		\
{									\
  mutex_lock (&component##_list_lock);					\
  if (component##_list_len == component##_list_alloc)			\
    {									\
      size_t new_alloc = component##_list_alloc + LIST_GROW;		\
      component##_t new_list = realloc (component##_list,		\
					new_alloc			\
					* sizeof (*component##_list));	\
      if (!new_list)							\
	{								\
	  mutex_unlock (&component##_list_lock);			\
	  return errno;							\
	}								\
      component##_list = new_list;					\
      component##_list_alloc = new_alloc;				\
    }									\
  component##_list[component##_list_len].ops = ops;			\
  component##_list[component##_list_len].handle = handle;		\
  component##_list_len++;						\
  mutex_unlock (&component##_list_lock);				\
  return 0;								\
}									\
									\
error_t									\
driver_remove_##component (component##_ops_t ops, void *handle)		\
{									\
  unsigned int i;								\
									\
  mutex_lock (&component##_list_lock);					\
  for (i = 0; i < component##_list_len; i++)				\
    if (component##_list[i].ops == ops					\
	&& component##_list[i].handle == handle)			\
      {									\
	while (i + 1 < component##_list_len)				\
	  {								\
	    component##_list[i] = component##_list[i + 1];		\
	    i++;							\
	  }								\
	component##_list_len--;						\
      }									\
  mutex_unlock (&component##_list_lock);				\
  return 0;								\
}

ADD_REMOVE_COMPONENT (display)
ADD_REMOVE_COMPONENT (input)
ADD_REMOVE_COMPONENT (bell)