Use CRITICAL job priority class for long running dispatcher jobs
[strongswan.git] / src / libcharon / plugins / nm / nm_plugin.c
1 /*
2 * Copyright (C) 2008-2009 Martin Willi
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 "nm_plugin.h"
17 #include "nm_service.h"
18 #include "nm_creds.h"
19 #include "nm_handler.h"
20
21 #include <hydra.h>
22 #include <daemon.h>
23 #include <processing/jobs/callback_job.h>
24
25 #define CAP_DAC_OVERRIDE 1
26
27 typedef struct private_nm_plugin_t private_nm_plugin_t;
28
29 /**
30 * private data of nm plugin
31 */
32 struct private_nm_plugin_t {
33
34 /**
35 * implements plugin interface
36 */
37 nm_plugin_t public;
38
39 /**
40 * NetworkManager service (VPNPlugin)
41 */
42 NMStrongswanPlugin *plugin;
43
44 /**
45 * Glib main loop for a thread, handles DBUS calls
46 */
47 GMainLoop *loop;
48
49 /**
50 * credential set registered at the daemon
51 */
52 nm_creds_t *creds;
53
54 /**
55 * attribute handler regeisterd at the daemon
56 */
57 nm_handler_t *handler;
58 };
59
60 /**
61 * NM plugin processing routine, creates and handles NMVPNPlugin
62 */
63 static job_requeue_t run(private_nm_plugin_t *this)
64 {
65 this->loop = g_main_loop_new(NULL, FALSE);
66 g_main_loop_run(this->loop);
67 return JOB_REQUEUE_NONE;
68 }
69
70 METHOD(plugin_t, get_name, char*,
71 private_nm_plugin_t *this)
72 {
73 return "nm";
74 }
75
76 METHOD(plugin_t, destroy, void,
77 private_nm_plugin_t *this)
78 {
79 if (this->loop)
80 {
81 if (g_main_loop_is_running(this->loop))
82 {
83 g_main_loop_quit(this->loop);
84 }
85 g_main_loop_unref(this->loop);
86 }
87 if (this->plugin)
88 {
89 g_object_unref(this->plugin);
90 }
91 lib->credmgr->remove_set(lib->credmgr, &this->creds->set);
92 hydra->attributes->remove_handler(hydra->attributes, &this->handler->handler);
93 this->creds->destroy(this->creds);
94 this->handler->destroy(this->handler);
95 free(this);
96 }
97
98 /*
99 * see header file
100 */
101 plugin_t *nm_plugin_create()
102 {
103 private_nm_plugin_t *this;
104
105 g_type_init ();
106 if (!g_thread_supported())
107 {
108 g_thread_init(NULL);
109 }
110
111 INIT(this,
112 .public = {
113 .plugin = {
114 .get_name = _get_name,
115 .reload = (void*)return_false,
116 .destroy = _destroy,
117 },
118 },
119 .creds = nm_creds_create(),
120 .handler = nm_handler_create(),
121 .plugin = nm_strongswan_plugin_new(this->creds, this->handler),
122 );
123
124 hydra->attributes->add_handler(hydra->attributes, &this->handler->handler);
125 lib->credmgr->add_set(lib->credmgr, &this->creds->set);
126 if (!this->plugin)
127 {
128 DBG1(DBG_CFG, "DBUS binding failed");
129 destroy(this);
130 return NULL;
131 }
132
133 /* bypass file permissions to read from users ssh-agent */
134 charon->keep_cap(charon, CAP_DAC_OVERRIDE);
135
136 lib->processor->queue_job(lib->processor,
137 (job_t*)callback_job_create_with_prio((callback_job_cb_t)run,
138 this, NULL, NULL, JOB_PRIO_CRITICAL));
139
140 return &this->public.plugin;
141 }
142