r13899: Get the shared library build building again. Just compiles for now,
[samba.git] / source / lib / com / tables.c
1 /*
2    Unix SMB/CIFS implementation.
3    COM class tables
4    Copyright (C) 2004 Jelmer Vernooij <jelmer@samba.org>
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "dlinklist.h"
23 #include "lib/com/com.h"
24
25 /* Specific implementation of one or more interfaces */
26 struct com_class
27 {
28         const char *progid;
29         struct GUID clsid;
30
31         struct IUnknown *class_object;
32         struct com_class *prev, *next;
33 } * running_classes = NULL;
34
35 static struct IUnknown *get_com_class_running(const struct GUID *clsid)
36 {
37         struct com_class *c = running_classes;
38
39         while(c) {
40
41                 if (GUID_equal(clsid, &c->clsid)) {
42                         return c->class_object;
43                 }
44
45                 c = c->next;
46         }
47
48         return NULL;
49 }
50
51 static struct IUnknown *get_com_class_so(TALLOC_CTX *mem_ctx, const struct GUID *clsid)
52 {
53         char *mod_name;
54         char *clsid_str;
55         void *mod;
56         get_class_object_function f;
57
58         clsid_str = GUID_string(mem_ctx, clsid);
59         mod_name = talloc_asprintf(mem_ctx, "%s.so", clsid_str);
60         talloc_free(clsid_str);
61
62         mod = dlopen(mod_name, 0);
63
64         if (!mod) {
65                 return NULL;
66         }
67         
68         f = dlsym(mod, "get_class_object");
69
70         if (!f) {
71                 return NULL;
72         }
73
74         return f(clsid);
75 }
76
77 struct IUnknown *com_class_by_clsid(struct com_context *ctx, const struct GUID *clsid)
78 {
79         struct IUnknown *c;
80         
81         /* Check list of running COM classes first */
82         c = get_com_class_running(clsid);
83
84         if (c != NULL) {
85                 return c;
86         }
87
88         c = get_com_class_so(ctx, clsid);
89
90         if (c != NULL) {
91                 return c;
92         }
93         
94         return NULL;
95 }
96
97 NTSTATUS com_register_running_class(struct GUID *clsid, const char *progid, struct IUnknown *p)
98 {
99         struct com_class *l = talloc_zero(running_classes?running_classes:talloc_autofree_context(), struct com_class);
100
101         l->clsid = *clsid;
102         l->progid = talloc_strdup(l, progid);
103         l->class_object = p;
104
105         DLIST_ADD(running_classes, l);
106         
107         return NT_STATUS_OK;
108 }