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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* rmvolmgr daemon
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <strings.h>
#include <errno.h>
#include <libintl.h>
#include <sys/syscall.h>
#include <libscf.h>
#include <priv_utils.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <libhal.h>
#include "rmm_common.h"
char *progname = "rmvolmgr";
#define RMVOLMGR_FMRI "svc:/system/filesystem/rmvolmgr:default"
typedef struct managed_volume {
char *udi;
boolean_t my;
struct action_arg aa;
} managed_volume_t;
static GSList *managed_volumes;
static GMainLoop *mainloop;
static LibHalContext *hal_ctx;
static int sigexit_pipe[2];
static GIOChannel *sigexit_ioch;
static boolean_t opt_c; /* disable CDE compatibility */
static boolean_t opt_n; /* disable legacy mountpoint symlinks */
static boolean_t opt_s; /* system instance */
/* SMF property "eject_button" */
static boolean_t rmm_prop_eject_button = B_TRUE;
static void get_smf_properties();
static void rmm_device_added(LibHalContext *ctx, const char *udi);
static void rmm_device_removed(LibHalContext *ctx, const char *udi);
static void rmm_property_modified(LibHalContext *ctx, const char *udi,
const char *key, dbus_bool_t is_removed, dbus_bool_t is_added);
static void rmm_device_condition(LibHalContext *ctx, const char *udi,
const char *name, const char *detail);
static void rmm_mount_all();
static void rmm_unmount_all();
static void sigexit(int signo);
static gboolean sigexit_ioch_func(GIOChannel *source, GIOCondition condition,
gpointer user_data);
static void
usage()
{
(void) fprintf(stderr, gettext("\nusage: rmvolmgr [-v]\n"));
}
static int
rmvolmgr(int argc, char **argv)
{
const char *opts = "chnsv";
DBusError error;
boolean_t daemonize;
rmm_error_t rmm_error;
int c;
while ((c = getopt(argc, argv, opts)) != EOF) {
switch (c) {
case 'c':
opt_c = B_TRUE;
break;
case 'n':
opt_n = B_TRUE;
break;
case 's':
opt_s = B_TRUE;
break;
case 'v':
rmm_debug = 1;
break;
case '?':
case 'h':
usage();
return (0);
default:
usage();
return (1);
}
}
if (opt_s) {
if (geteuid() != 0) {
(void) fprintf(stderr,
gettext("system instance must have euid 0\n"));
return (1);
}
get_smf_properties();
if (opt_c) {
rmm_vold_actions_enabled = B_FALSE;
}
if (opt_n) {
rmm_vold_mountpoints_enabled = B_FALSE;
}
/*
* Drop unused privileges. Remain root for HAL interaction
* and to create legacy symlinks.
*
* Need PRIV_FILE_DAC_WRITE to write to users'
* /tmp/.removable/notify* files.
*/
if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET,
0, 0,
rmm_vold_actions_enabled ? PRIV_FILE_DAC_WRITE : NULL,
NULL) == -1) {
(void) fprintf(stderr,
gettext("failed to drop privileges"));
return (1);
}
/* basic privileges we don't need */
(void) priv_set(PRIV_OFF, PRIV_PERMITTED, PRIV_PROC_EXEC,
PRIV_PROC_INFO, PRIV_FILE_LINK_ANY, PRIV_PROC_SESSION,
NULL);
} else {
if (opt_c) {
rmm_vold_actions_enabled = B_FALSE;
}
if (opt_n) {
rmm_vold_mountpoints_enabled = B_FALSE;
}
}
daemonize = (getenv("RMVOLMGR_NODAEMON") == NULL);
if (daemonize && daemon(0, 0) < 0) {
dprintf("daemonizing failed: %s", strerror(errno));
return (1);
}
if (opt_s) {
__fini_daemon_priv(PRIV_PROC_FORK, NULL);
}
/*
* signal mainloop integration using pipes
*/
if (pipe(sigexit_pipe) != 0) {
dprintf("pipe failed %s\n", strerror(errno));
return (1);
}
sigexit_ioch = g_io_channel_unix_new(sigexit_pipe[0]);
if (sigexit_ioch == NULL) {
dprintf("g_io_channel_unix_new failed\n");
return (1);
}
g_io_add_watch(sigexit_ioch, G_IO_IN, sigexit_ioch_func, NULL);
signal(SIGTERM, sigexit);
signal(SIGINT, sigexit);
signal(SIGHUP, SIG_IGN);
signal(SIGUSR1, SIG_IGN);
signal(SIGUSR2, SIG_IGN);
if ((hal_ctx = rmm_hal_init(rmm_device_added, rmm_device_removed,
rmm_property_modified, rmm_device_condition,
&error, &rmm_error)) == NULL) {
dbus_error_free(&error);
return (1);
}
/* user instance should claim devices */
if (!opt_s) {
if (!rmm_hal_claim_branch(hal_ctx, HAL_BRANCH_LOCAL)) {
(void) fprintf(stderr,
gettext("cannot claim branch\n"));
return (1);
}
}
rmm_mount_all();
if ((mainloop = g_main_loop_new(NULL, B_FALSE)) == NULL) {
dprintf("Cannot create main loop\n");
return (1);
}
g_main_loop_run(mainloop);
return (0);
}
static void
get_smf_properties()
{
scf_simple_prop_t *prop;
uint8_t *val;
if ((prop = scf_simple_prop_get(NULL, RMVOLMGR_FMRI,
"rmvolmgr", "legacy_mountpoints")) != NULL) {
if ((val = scf_simple_prop_next_boolean(prop)) != NULL) {
rmm_vold_mountpoints_enabled = (*val != 0);
}
scf_simple_prop_free(prop);
}
if ((prop = scf_simple_prop_get(NULL, RMVOLMGR_FMRI,
"rmvolmgr", "cde_compatible")) != NULL) {
if ((val = scf_simple_prop_next_boolean(prop)) != NULL) {
rmm_vold_actions_enabled = (*val != 0);
}
scf_simple_prop_free(prop);
}
if ((prop = scf_simple_prop_get(NULL, RMVOLMGR_FMRI,
"rmvolmgr", "eject_button")) != NULL) {
if ((val = scf_simple_prop_next_boolean(prop)) != NULL) {
rmm_prop_eject_button = (*val != 0);
}
scf_simple_prop_free(prop);
}
}
/* ARGSUSED */
static void
sigexit(int signo)
{
dprintf("signal to exit %d\n", signo);
write(sigexit_pipe[1], "s", 1);
}
/* ARGSUSED */
static gboolean
sigexit_ioch_func(GIOChannel *source, GIOCondition condition,
gpointer user_data)
{
gchar buf[1];
gsize bytes_read;
GError *error = NULL;
if (g_io_channel_read_chars(source, buf, 1, &bytes_read, &error) !=
G_IO_STATUS_NORMAL) {
dprintf("g_io_channel_read_chars failed %s", error->message);
g_error_free(error);
return (TRUE);
}
dprintf("signal to exit\n");
rmm_unmount_all();
g_main_loop_quit(mainloop);
return (TRUE);
}
static managed_volume_t *
rmm_managed_alloc(LibHalContext *ctx, const char *udi)
{
managed_volume_t *v;
if ((v = calloc(1, sizeof (managed_volume_t))) == NULL) {
return (NULL);
}
if ((v->udi = strdup(udi)) == NULL) {
free(v);
return (NULL);
}
if (!rmm_volume_aa_from_prop(ctx, udi, NULL, &v->aa)) {
free(v->udi);
free(v);
return (NULL);
}
return (v);
}
static void
rmm_managed_free(managed_volume_t *v)
{
rmm_volume_aa_free(&v->aa);
free(v->udi);
free(v);
}
static gint
rmm_managed_compare_udi(gconstpointer a, gconstpointer b)
{
const managed_volume_t *va = a;
const char *udi = b;
return (strcmp(va->udi, udi));
}
static boolean_t
volume_should_mount(const char *udi)
{
char *storage_device = NULL;
int ret = B_FALSE;
if (libhal_device_get_property_bool(hal_ctx, udi,
"volume.ignore", NULL)) {
goto out;
}
/* get the backing storage device */
if (!(storage_device = libhal_device_get_property_string(hal_ctx, udi,
"block.storage_device", NULL))) {
dprintf("cannot get block.storage_device\n");
goto out;
}
/* we handle either removable or hotpluggable */
if (!libhal_device_get_property_bool(hal_ctx, storage_device,
"storage.removable", NULL) &&
!libhal_device_get_property_bool(hal_ctx, storage_device,
"storage.hotpluggable", NULL)) {
goto out;
}
/* ignore if claimed by another volume manager */
if (libhal_device_get_property_bool(hal_ctx, storage_device,
"info.claimed", NULL)) {
goto out;
}
ret = B_TRUE;
out:
libhal_free_string(storage_device);
return (ret);
}
static void
volume_added(const char *udi)
{
GSList *l;
managed_volume_t *v;
dprintf("volume added %s\n", udi);
l = g_slist_find_custom(managed_volumes, udi, rmm_managed_compare_udi);
v = (l != NULL) ? l->data : NULL;
if (v != NULL) {
dprintf("already managed %s\n", udi);
return;
}
if (!volume_should_mount(udi)) {
dprintf("should not mount %s\n", udi);
return;
}
if ((v = rmm_managed_alloc(hal_ctx, udi)) == NULL) {
return;
}
if (rmm_action(hal_ctx, udi, INSERT, &v->aa, 0, 0, 0)) {
v->my = B_TRUE;
managed_volumes = g_slist_prepend(managed_volumes, v);
} else {
dprintf("rmm_action failed %s\n", udi);
rmm_managed_free(v);
}
}
static void
volume_removed(const char *udi)
{
GSList *l;
managed_volume_t *v;
dprintf("volume removed %s\n", udi);
l = g_slist_find_custom(managed_volumes, udi, rmm_managed_compare_udi);
v = (l != NULL) ? l->data : NULL;
if (v == NULL) {
return;
}
/* HAL will unmount, just do the vold legacy stuff */
v->aa.aa_action = EJECT;
(void) vold_postprocess(hal_ctx, udi, &v->aa);
rmm_managed_free(v);
managed_volumes = g_slist_delete_link(managed_volumes, l);
}
/* ARGSUSED */
static void
rmm_device_added(LibHalContext *ctx, const char *udi)
{
if (libhal_device_query_capability(hal_ctx, udi, "volume", NULL)) {
volume_added(udi);
}
}
/* ARGSUSED */
static void
rmm_device_removed(LibHalContext *ctx, const char *udi)
{
if (libhal_device_query_capability(hal_ctx, udi, "volume", NULL)) {
volume_removed(udi);
}
}
/* ARGSUSED */
static void
rmm_property_modified(LibHalContext *ctx, const char *udi, const char *key,
dbus_bool_t is_removed, dbus_bool_t is_added)
{
DBusError error;
GSList *l;
managed_volume_t *v;
boolean_t is_mounted;
if (strcmp(key, "volume.is_mounted") != 0) {
return;
}
is_mounted = libhal_device_get_property_bool(hal_ctx, udi, key, NULL);
l = g_slist_find_custom(managed_volumes, udi, rmm_managed_compare_udi);
v = (l != NULL) ? l->data : NULL;
if (is_mounted) {
dprintf("Mounted: %s\n", udi);
if (v != NULL) {
/* volume mounted by us is already taken care of */
if (v->my) {
return;
}
} else {
if ((v = rmm_managed_alloc(ctx, udi)) == NULL) {
return;
}
managed_volumes = g_slist_prepend(managed_volumes, v);
}
v->aa.aa_action = INSERT;
(void) vold_postprocess(hal_ctx, udi, &v->aa);
} else {
dprintf("Unmounted: %s\n", udi);
if (v == NULL) {
return;
}
v->aa.aa_action = EJECT;
(void) vold_postprocess(hal_ctx, udi, &v->aa);
rmm_managed_free(v);
managed_volumes = g_slist_delete_link(managed_volumes, l);
}
}
static void
storage_eject_pressed(const char *udi)
{
DBusError error;
/* ignore if disabled via SMF or claimed by another volume manager */
if (!rmm_prop_eject_button ||
libhal_device_get_property_bool(hal_ctx, udi, "info.claimed",
NULL)) {
return;
}
dbus_error_init(&error);
(void) rmm_hal_eject(hal_ctx, udi, &error);
rmm_dbus_error_free(&error);
}
/* ARGSUSED */
static void
rmm_device_condition(LibHalContext *ctx, const char *udi,
const char *name, const char *detail)
{
if ((strcmp(name, "EjectPressed") == 0) &&
libhal_device_query_capability(hal_ctx, udi, "storage", NULL)) {
storage_eject_pressed(udi);
}
}
/*
* Mount all mountable volumes
*/
static void
rmm_mount_all()
{
DBusError error;
char **udis = NULL;
int num_udis;
int i;
managed_volume_t *v;
dbus_error_init(&error);
/* get all volumes */
if ((udis = libhal_find_device_by_capability(hal_ctx, "volume",
&num_udis, &error)) == NULL) {
dprintf("mount_all: no volumes found\n");
goto out;
}
for (i = 0; i < num_udis; i++) {
/* skip if already mounted */
if (libhal_device_get_property_bool(hal_ctx, udis[i],
"volume.is_mounted", NULL)) {
dprintf("mount_all: %s already mounted\n", udis[i]);
continue;
}
if (!volume_should_mount(udis[i])) {
continue;
}
if ((v = rmm_managed_alloc(hal_ctx, udis[i])) == NULL) {
continue;
}
if (rmm_action(hal_ctx, udis[i], INSERT, &v->aa, 0, 0, 0)) {
v->my = B_TRUE;
managed_volumes = g_slist_prepend(managed_volumes, v);
} else {
rmm_managed_free(v);
}
}
out:
if (udis != NULL) {
libhal_free_string_array(udis);
}
rmm_dbus_error_free(&error);
}
/*
* Mount all volumes mounted by this program
*/
static void
rmm_unmount_all()
{
GSList *i;
managed_volume_t *v;
for (i = managed_volumes; i != NULL; i = managed_volumes) {
v = (managed_volume_t *)i->data;
if (v->my && libhal_device_get_property_bool(hal_ctx, v->udi,
"volume.is_mounted", NULL)) {
(void) rmm_action(hal_ctx, v->udi, UNMOUNT,
&v->aa, 0, 0, 0);
}
managed_volumes = g_slist_remove(managed_volumes, v);
rmm_managed_free(v);
}
}
int
main(int argc, char **argv)
{
vold_init(argc, argv);
return (rmvolmgr(argc, argv));
}
|