xpc: add a description of the basic XPC protocol to README
[strongswan.git] / src / libhydra / hydra.c
1 /*
2 * Copyright (C) 2010 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "hydra.h"
17
18 #include <utils/debug.h>
19
20 typedef struct private_hydra_t private_hydra_t;
21
22 /**
23 * Private additions to hydra_t.
24 */
25 struct private_hydra_t {
26
27 /**
28 * Public members of hydra_t.
29 */
30 hydra_t public;
31
32 /**
33 * Integrity check failed?
34 */
35 bool integrity_failed;
36
37 /**
38 * Number of times we have been initialized
39 */
40 refcount_t ref;
41 };
42
43 /**
44 * Single instance of hydra_t.
45 */
46 hydra_t *hydra = NULL;
47
48 /**
49 * Described in header.
50 */
51 void libhydra_deinit()
52 {
53 private_hydra_t *this = (private_hydra_t*)hydra;
54
55 if (!this || !ref_put(&this->ref))
56 { /* have more users */
57 return;
58 }
59
60 this->public.attributes->destroy(this->public.attributes);
61 this->public.kernel_interface->destroy(this->public.kernel_interface);
62 free((void*)this->public.daemon);
63 free(this);
64 hydra = NULL;
65 }
66
67 /**
68 * Described in header.
69 */
70 bool libhydra_init(const char *daemon)
71 {
72 private_hydra_t *this;
73
74 if (hydra)
75 { /* already initialized, increase refcount */
76 this = (private_hydra_t*)hydra;
77 ref_get(&this->ref);
78 return !this->integrity_failed;
79 }
80
81 INIT(this,
82 .public = {
83 .attributes = attribute_manager_create(),
84 .daemon = strdup(daemon ?: "libhydra"),
85 },
86 .ref = 1,
87 );
88 hydra = &this->public;
89
90 this->public.kernel_interface = kernel_interface_create();
91
92 if (lib->integrity &&
93 !lib->integrity->check(lib->integrity, "libhydra", libhydra_init))
94 {
95 DBG1(DBG_LIB, "integrity check of libhydra failed");
96 this->integrity_failed = TRUE;
97 }
98 return !this->integrity_failed;
99 }