r5490: The big (D)COM commit! :-) Contains most of the changes described in the
[samba.git] / source4 / 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
24 /* Specific implementation of one or more interfaces */
25 struct com_class
26 {
27         const char *progid;
28         struct GUID clsid;
29
30         struct IUnknown *class_object;
31         struct com_class *prev, *next;
32 } * running_classes = NULL;
33
34 static struct IUnknown *get_com_class_running(const struct GUID *clsid)
35 {
36         struct com_class *c = running_classes;
37
38         while(c) {
39
40                 if (GUID_equal(clsid, &c->clsid)) {
41                         return c->class_object;
42                 }
43
44                 c = c->next;
45         }
46
47         return NULL;
48 }
49
50 static struct IUnknown *get_com_class_so(TALLOC_CTX *mem_ctx, const struct GUID *clsid)
51 {
52         char *mod_name;
53         char *clsid_str;
54         void *mod;
55         get_class_object_function f;
56
57         clsid_str = GUID_string(mem_ctx, clsid);
58         mod_name = talloc_asprintf(mem_ctx, "%s.so", clsid_str);
59         talloc_free(clsid_str);
60
61         mod = sys_dlopen(mod_name, 0);
62
63         if (!mod) {
64                 return NULL;
65         }
66         
67         f = sys_dlsym(mod, "get_class_object");
68
69         if (!f) {
70                 return NULL;
71         }
72
73         return f(clsid);
74 }
75
76 struct IUnknown *com_class_by_clsid(struct com_context *ctx, const struct GUID *clsid)
77 {
78         struct IUnknown *c;
79         
80         /* Check list of running COM classes first */
81         c = get_com_class_running(clsid);
82
83         if (c != NULL) {
84                 return c;
85         }
86
87         c = get_com_class_so(ctx, clsid);
88
89         if (c != NULL) {
90                 return c;
91         }
92         
93         return NULL;
94 }
95
96 NTSTATUS com_register_running_class(struct GUID *clsid, const char *progid, struct IUnknown *p)
97 {
98         struct com_class *l = talloc_zero(running_classes?running_classes:talloc_autofree_context(), struct com_class);
99
100         l->clsid = *clsid;
101         l->progid = talloc_strdup(l, progid);
102         l->class_object = p;
103
104         DLIST_ADD(running_classes, l);
105         
106         return NT_STATUS_OK;
107 }