File: control.c

package info (click to toggle)
mountall 2.46
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,124 kB
  • sloc: ansic: 16,780; sh: 12,354; makefile: 631; yacc: 291; xml: 50; sed: 16
file content (258 lines) | stat: -rw-r--r-- 7,096 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* mountall 
 *
 * Copyright © 2010 Canonical Ltd.
 * Author: Surbhi A. Palande <surbhi.palande@ubuntu.com>
 *
 * This file is based on control.c in upstart whose
 * Author: Scott James Remnant <scott@netsplit.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <dbus/dbus.h>

#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/string.h>
#include <nih/list.h>
#include <nih/io.h>
#include <nih/main.h>
#include <nih/logging.h>
#include <nih/error.h>
#include <nih/errors.h>

#include <nih-dbus/dbus_error.h>
#include <nih-dbus/dbus_connection.h>
#include <nih-dbus/dbus_message.h>
#include <nih-dbus/dbus_object.h>

#include "dbus/mountall.h"
#include "com.ubuntu.Mountall.Server.h"
#include "mountall.h"

extern const char *package_string;

/* Prototypes for static functions */
static int   control_server_connect (DBusServer *server, DBusConnection *conn);
static void  control_disconnected   (DBusConnection *conn);
static void  control_register_all   (DBusConnection *conn);

/**
 * control_server
 * 
 * D-Bus server listening for new direct connections.
 **/
DBusServer *control_server = NULL;


/**
 * control_server_address:
 *
 * Address on which the control server may be reached.
 **/
const char * control_server_address = DBUS_ADDRESS_MNTALL;

/**
 * control_server_open:
 *
 * Open a listening D-Bus server and store it in the control_server global.
 * New connections are permitted from the root user, and handled
 * automatically in the main loop.
 *
 * Returns: zero on success, negative value on raised error.
 **/

int
control_server_open (void)
{
	nih_assert (control_server == NULL);

	control_server = nih_dbus_server (control_server_address,
				  control_server_connect,
				  control_disconnected);
	if (! control_server)
		return -1;

	nih_debug("Mountall0_1.Server started at address: %s", control_server_address);

	return 0;
}

/**
 * control_server_connect:
 *
 * Called when a new client connects to our server and is used to register
 * objects on the new connection.
 *
 * Returns: always TRUE.
 **/
static int
control_server_connect (DBusServer     *server,
			DBusConnection *conn)
{
	nih_assert (server != NULL);
	nih_assert (server == control_server);
	nih_assert (conn != NULL);

	/* Register objects on the connection. */
	control_register_all (conn);

	nih_debug("Mountall0_1.Server::Connection from private client");

	return TRUE;
}

/**
 * control_register_all:
 * @conn: connection to register objects for.
 *
 * Registers the manager object and objects for all jobs and instances on
 * the given connection.
 **/
static void
control_register_all (DBusConnection *conn)
{
	nih_assert (conn != NULL);

	/* Register the manager object, this is the primary point of contact
	 * for clients.  We only check for success, otherwise we're happy
	 * to let this object be tied to the lifetime of the connection.
	 */
	NIH_MUST (nih_dbus_object_new (NULL, conn, DBUS_PATH_MNTALL,
				       control_interfaces, NULL));
}

/**
 * control_disconnected:
 *
 * This function is called when the connection to the D-Bus system bus,
 * or a client connection to our D-Bus server, is dropped and our reference
 * is about to be list.  We clear the connection from our current list
 * and drop the control_bus global if relevant.
 **/
static void
control_disconnected (DBusConnection *conn)
{
	nih_assert (conn != NULL);
}

/**
 * control_stop_timer:
 * @mountpoint: the mountpoint corresponding to the device for which the
 * timeout option has to be disabled.
 *
 * Implements the StopTimer method of com.ubuntu.Moutall01_Server
 * interface.
 *
 * This function is called for stopping a previously started timer for a
 * mountpoint. Stopping a timer has the effect that mountall would no longer
 * expect the corresponding device to be ready within a previously registered
 * stiplulated time period. After this call, mountall will wait endlessly till
 * the device becomes ready. Use this function only when you know that you
 * might restart the timer later or that you really want to wait endlessly
 * till the device becomes available.
 *
 * Returns 0 on success and -1 on failure.
 **/
int
control_stop_timer  (void *data, 
		     NihDBusMessage *message,
		     const char *mountpoint)
{

	nih_assert (mountpoint != NULL);
	nih_assert (message != NULL);

	return stop_dev_timer (mountpoint);
}

/**
 * control_restart_timer:
 * @mountpoint: the mountpoint corresponding to the device for which the
 * timeout option has to be disabled.
 *
 * Implements the RestartTimer method of com.ubuntu.Moutall01_Server
 * interface.
 *
 * This function is called for restarting a previously stopped timer for a
 * mountpoint. After successfully restarting a timer, mountall will expect the
 * corresponding device to become ready for mounting within the previously
 * configured timeout or the default of 30 seconds.
 *
 * Returns 0 on success and -1 on failure.
 **/
int
control_restart_timer (void *data,
		       NihDBusMessage *message,
		       const char *mountpoint)
{

	nih_assert (mountpoint != NULL);
	nih_assert (message != NULL);

	return restart_dev_timer (mountpoint);
}

/**
 * control_change_mount_device:
 * @devname: Name of the new device which you want to mount at @path.
 * @path: Complete path which matches with the one found in /etc/fstab or what
 * mountall already considers.
 *
 * Implements the ChangeMountDevice method of com.ubuntu.Moutall01_Server
 * interface.
 *
 * Call this function to change the device to mount to an existing previous
 * mountpoint.
 **/
int
control_change_mount_device (void *data, 
		       NihDBusMessage *message,
		       const char *devname,
		       const char *path)
{
	nih_assert (devname != NULL);
	nih_assert (path != NULL);

	return change_mount_device (devname, path);
}

/**
 * control_get_version:
 * @data: not used,
 * @message: D-Bus connection and message received,
 * @version: pointer for reply string.
 *
 * Implements the get method for the version property of the
 * com.ubuntu.Mountall0_1.Server interface.
 *
 * Called to obtain the version of the init daemon, which will be stored
 * as a string in @version.
 *
 * Returns: zero on success, negative value on raised error.
 **/
int
control_get_version (void *data,
	             NihDBusMessage *message,
		     char **version)
{
	nih_assert (message != NULL);
	nih_assert (version != NULL);

	*version = nih_strdup (message, package_string);
	if (!*version)
		nih_return_no_memory_error (-1);

	return 0;
}