Line data Source code
1 : /*
2 : Authors:
3 : Simo Sorce <ssorce@redhat.com>
4 :
5 : Copyright (C) 2009 Red Hat
6 :
7 : This program is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3 of the License, or
10 : (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include <talloc.h>
22 :
23 : #include "util/util.h"
24 :
25 : /*
26 : * sssd_mem_attach
27 : * This function will take a non-talloc pointer and "attach" it to a talloc
28 : * memory context. It will accept a destructor for the original pointer
29 : * so that when the parent memory context is freed, the non-talloc
30 : * pointer will also be freed properly.
31 : */
32 :
33 0 : int password_destructor(void *memctx)
34 : {
35 0 : char *password = (char *)memctx;
36 : int i;
37 :
38 : /* zero out password */
39 0 : for (i = 0; password[i]; i++) password[i] = '\0';
40 :
41 0 : return 0;
42 : }
43 :
44 0 : static int mem_holder_destructor(void *ptr)
45 : {
46 : struct mem_holder *h;
47 :
48 0 : h = talloc_get_type(ptr, struct mem_holder);
49 0 : return h->fn(h->mem);
50 : }
51 :
52 0 : void *sss_mem_attach(TALLOC_CTX *mem_ctx,
53 : void *ptr,
54 : void_destructor_fn_t *fn)
55 : {
56 : struct mem_holder *h;
57 :
58 0 : if (!ptr || !fn) return NULL;
59 :
60 0 : h = talloc(mem_ctx, struct mem_holder);
61 0 : if (!h) return NULL;
62 :
63 0 : h->mem = ptr;
64 0 : h->fn = fn;
65 0 : talloc_set_destructor((TALLOC_CTX *)h, mem_holder_destructor);
66 :
67 0 : return h;
68 : }
|