summaryrefslogtreecommitdiff
path: root/usr/src/cmd/rmvolmgr/vold.c
blob: 30a2f267e8476254c6b35f14d658d97d13f340be (plain)
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
/*
 * 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.
 */

/*
 * Vold compatibility for rmvolmgr: emulate old commands as well as
 * action_filemgr.so to notify legacy apps via /tmp/.removable pipes.
 * A lot of this code is copied verbatim from vold sources.
 *
 * Here's the original description of action_filemgr.so:
 *
 * action_filemgr.so - filemgr interface routines for rmmount
 *
 * This shared object allows rmmount to communicate with filemgr.
 * This is done by communicating over a named pipe that filemgr
 * creates in directory NOTIFY_DIR.  The name of the pipe must
 * begin with NOTIFY_NAME.  This source file contains #define
 * compiler directives set the values of NOTIFY_DIR and NOTIFY_NAME.
 *
 * After a partition on a medium has been mounted as a result of
 * either insertion or remounting of the medium, the action()
 * method creates a file named with the symbolic name of the
 * device in which the medium is inserted and the partition name
 * (e.g. "jaz0-s2") in NOTIFY_DIR.  The file consists of one text
 * line containing a string naming the mount point of the partition,
 * a string giving the raw device path to the partition, and a
 * string naming the file system type on the partition.  The action()
 * method then sends a single character ('i' for insertion, 'r' for
 * remounting) through the named pipe NOTIFY_NAME to tell filemgr to
 * look for new files in NOTIFY_DIR.
 *
 * If a medium containing no mountable partitions is inserted
 * or remounted in a device, the action() method creates a file
 * named with the symbolic name of the device in NOTIFY_DIR.
 * The file consists of one text line containing a string
 * giving the symbolic name of the device and a string naming
 * the reason that the medium couldn't be mounted.  The action
 * method then sends either an 'i' or an 'r' through the named
 * pipe to tell filemgr to look for new files in NOTIFY_DIR.
 *
 * When a medium is ejected or unmounted, the action() method
 * removes the files that were created in NOTIFY_DIR when the medium
 * was inserted or remounted and sends a single character ('e' for
 * ejection, 'u' for unmounting) through the named pipe.
 *
 * The following environment variables must be set before calling action():
 *
 *	VOLUME_ACTION		action that occurred (e.g. "insert", "eject")
 *	VOLUME_SYMDEV		symbolic name (e.g. "cdrom0", "floppy1")
 *	VOLUME_NAME		volume name (e.g. "unnamed_cdrom", "s2")
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include <dirent.h>
#include <signal.h>
#include <errno.h>
#include <libintl.h>
#include <zone.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/dkio.h>
#include <sys/cdio.h>
#include <sys/vtoc.h>
#include <sys/param.h>
#include <sys/wait.h>
#include <libcontract.h>
#include <sys/contract/process.h>
#include <sys/ctfs.h>
#include <tsol/label.h>

#include "vold.h"
#include "rmm_common.h"

int		rmm_debug = 0;
boolean_t	rmm_vold_actions_enabled = B_FALSE;
boolean_t	rmm_vold_mountpoints_enabled = B_FALSE;

static char	*prog_name = NULL;
static pid_t	prog_pid = 0;
static int	system_labeled = 0;
static uid_t	mnt_uid = (uid_t)-1;
static gid_t	mnt_gid = (gid_t)-1;
static zoneid_t	mnt_zoneid = -1;
static char	mnt_zoneroot[MAXPATHLEN];
static char	mnt_userdir[MAXPATHLEN];

/*
 * Private attribute types and attributes.
 */
static const char notify_characters[] = {
	'e',
	'i',
	'r',
	'u'
};

static const char *result_strings[] = {
	"FALSE",
	"TRUE"
};

#define	NOTIFY_DIR	"/tmp/.removable"	/* dir where filemgr looks */
#define	NOTIFY_NAME	"notify"		/* named pipe to talk over */

static void	volrmmount_usage();
static void	volcheck_usage();
static int	vold_action(struct action_arg *aap);
static void	vold_update_mountpoints(struct action_arg *aap);
static char	*not_mountable(struct action_arg *aa);
static int	create_one_notify_file(char *fstype,
				char *mount_point,
				char *notify_file,
				char *raw_partitionp,
				char *reason,
				char *symdev);
static int	create_notify_files(struct action_arg **aa);
static boolean_t notify_clients(action_t action, int do_notify);
static void	popdir(int fd);
static int	pushdir(const char *dir);
static boolean_t remove_notify_files(struct action_arg **aa);

/*
 * should be called once from main()
 */
/* ARGSUSED */
void
vold_init(int argc, char **argv)
{
	system_labeled = is_system_labeled();
}

/*
 * Old version of rmmount(1M)
 */
/* ARGSUSED */
int
vold_rmmount(int argc, char **argv)
{
	char		*volume_action;
	char		*volume_mediatype;
	char		*volume_mount_mode;
	char		*volume_name;
	char		*volume_path;
	char		*volume_pcfs_id;
	char		*volume_symdev;
	char		*volume_zonename;
	char		*volume_user;
	action_t	action;
	char		mountpoint[MAXPATHLEN];
	char		*zonemountpoint;
	char		*arg_mountpoint = NULL;
	LibHalContext	*hal_ctx;
	DBusError	error;
	rmm_error_t	rmm_error;
	int		ret;

	prog_name = argv[0];
	prog_pid = getpid();

	mnt_zoneroot[0] = '\0';
	mnt_userdir[0] = '\0';

	volume_action = getenv("VOLUME_ACTION");
	volume_mediatype = getenv("VOLUME_MEDIATYPE");
	volume_mount_mode = getenv("VOLUME_MOUNT_MODE");
	volume_name = getenv("VOLUME_NAME");
	volume_path = getenv("VOLUME_PATH");
	volume_pcfs_id = getenv("VOLUME_PCFS_ID");
	volume_symdev = getenv("VOLUME_SYMDEV");

	if (system_labeled) {
		volume_zonename = getenv("VOLUME_ZONE_NAME");
		volume_user = getenv("VOLUME_USER");
	}
	if (volume_action == NULL) {
		dprintf("%s(%ld): VOLUME_ACTION was null!!\n",
		    prog_name, prog_pid);
		return (-1);
	}
	if (volume_mediatype == NULL) {
		dprintf("%s(%ld): VOLUME_MEDIATYPE was null!!\n",
		    prog_name, prog_pid);
		return (-1);
	}
	if (volume_mount_mode == NULL) {
		volume_mount_mode = "rw";
	}
	if (volume_name == NULL) {
		dprintf("%s(%ld): VOLUME_NAME was null!!\n",
		    prog_name, prog_pid);
		return (-1);
	}
	if (volume_path == NULL) {
		dprintf("%s(%ld): VOLUME_PATH was null!!\n",
		    prog_name, prog_pid);
		return (-1);
	}
	if (volume_pcfs_id == NULL) {
		volume_pcfs_id = "";
	}
	if (volume_symdev == NULL) {
		dprintf("%s(%ld): VOLUME_SYMDEV was null!!\n",
		    prog_name, prog_pid);
		return (-1);
	}

	if (system_labeled) {
		if (volume_zonename != NULL &&
		    strcmp(volume_zonename, GLOBAL_ZONENAME) != 0) {
			if ((mnt_zoneid =
			    getzoneidbyname(volume_zonename)) != -1) {
				if (zone_getattr(mnt_zoneid, ZONE_ATTR_ROOT,
				    mnt_zoneroot, MAXPATHLEN) == -1) {
					dprintf("%s(%ld): NO ZONEPATH!!\n",
					    prog_name, prog_pid);
					return (-1);
				}
			}
		} else {
			mnt_zoneid = GLOBAL_ZONEID;
			mnt_zoneroot[0] = '\0';
		}
		if (volume_user != NULL) {
			struct passwd	 *pw;

			if ((pw = getpwnam(volume_user)) == NULL) {
				dprintf("%s(%ld) %s\n", prog_name, prog_pid,
				    ": VOLUME_USER was not a valid user!");
				return (-1);
			}
			mnt_uid = pw->pw_uid;
			mnt_gid = pw->pw_gid;

			if (snprintf(mnt_userdir, sizeof (mnt_userdir),
			    "/%s-%s", volume_user, volume_symdev) >=
			    sizeof (mnt_userdir))
				return (-1);
		} else {
			mnt_uid = 0;
			mnt_userdir[0] = '\0';
		}

		rmm_vold_mountpoints_enabled = B_FALSE;
		rmm_vold_actions_enabled = B_TRUE;
	} else {
		rmm_vold_mountpoints_enabled = B_TRUE;
		rmm_vold_actions_enabled = B_TRUE;
	}

	if ((hal_ctx = rmm_hal_init(0, 0, 0, 0, &error, &rmm_error)) == NULL) {
		rmm_dbus_error_free(&error);

		/* if HAL's not running, must be root */
		if (geteuid() != 0) {
			(void) fprintf(stderr,
			    gettext("%s(%ld) error: must be root to execute\n"),
			    prog_name, prog_pid);
			return (-1);
		}
	}

	if (strcmp(volume_action, "eject") == 0) {
		action = EJECT;
	} else if (strcmp(volume_action, "insert") == 0) {
		action = INSERT;

		if (system_labeled) {
			/*
			 * create mount point
			 */
			if (strlen(mnt_userdir) > 0) {
				if (snprintf(mountpoint, MAXPATHLEN,
				    "%s/%s%s", mnt_zoneroot, volume_mediatype,
				    mnt_userdir) > MAXPATHLEN) {
					return (-1);

				}
				(void) makepath(mountpoint, 0700);
				(void) chown(mountpoint, mnt_uid, mnt_gid);
				/*
				 * set the top level directory bits to 0755
				 * so user can access it.
				 */
				if (snprintf(mountpoint, MAXPATHLEN,
				    "%s/%s", mnt_zoneroot,
				    volume_mediatype) <= MAXPATHLEN) {
					(void) chmod(mountpoint, 0755);
				}
			}
			if (snprintf(mountpoint, MAXPATHLEN,
			    "%s/%s%s/%s", mnt_zoneroot, volume_mediatype,
			    mnt_userdir, volume_name) > MAXPATHLEN) {
				(void) fprintf(stderr,
				    gettext("%s(%ld) error: path too long\n"),
				    prog_name, prog_pid);
				return (-1);
			}

			/* make our mountpoint */
			(void) makepath(mountpoint, 0755);

			arg_mountpoint = mountpoint;
		}
	} else if (strcmp(volume_action, "remount") == 0) {
		action = REMOUNT;
	} else if (strcmp(volume_action, "unmount") == 0) {
		action = UNMOUNT;
	}

	ret = rmm_action(hal_ctx, volume_symdev, action, 0, 0, 0,
	    arg_mountpoint) ? 0 : 1;

	if (hal_ctx != NULL) {
		rmm_hal_fini(hal_ctx);
	}

	return (ret);
}


/*
 * this should be called after rmm_hal_{mount,unmount,eject}
 */
int
vold_postprocess(LibHalContext *hal_ctx, const char *udi,
    struct action_arg *aap)
{
	int	ret = 0;

	/* valid mountpoint required */
	if ((aap->aa_action == INSERT) || (aap->aa_action == REMOUNT)) {
		rmm_volume_aa_update_mountpoint(hal_ctx, udi, aap);
		if ((aap->aa_mountpoint == NULL) ||
		    (strlen(aap->aa_mountpoint) == 0)) {
			return (1);
		}
	}

	if (rmm_vold_mountpoints_enabled) {
		vold_update_mountpoints(aap);
	}
	if (rmm_vold_actions_enabled) {
		ret = vold_action(aap);
	}

	return (ret);
}

/*
 * update legacy symlinks
 *
 * For cdrom:
 *
 *	/cdrom/<name> -> original mountpoint
 *	/cdrom/cdrom0 -> ./<name>
 *	/cdrom/cdrom -> cdrom0  (only for cdrom0)
 *
 * If it's a slice or partition, /cdrom/<name> becomes a directory:
 *
 *	/cdrom/<name>/s0
 *
 * Same for rmdisk and floppy.
 *
 * On labeled system (Trusted Solaris), links are in a user directory.
 */
static void
vold_update_mountpoints(struct action_arg *aap)
{
	boolean_t	is_partition;
	char		part_dir[2 * MAXNAMELEN];
	char		symname_mp[2 * MAXNAMELEN];
	char		symcontents_mp[MAXNAMELEN];
	char		symname[2 * MAXNAMELEN];
	char		symcontents[MAXNAMELEN];

	is_partition = (aap->aa_partname != NULL);

	if (!system_labeled) {
		if (!is_partition) {
			/* /cdrom/<name> -> original mountpoint */
			(void) snprintf(symcontents_mp, sizeof (symcontents_mp),
			    "%s", aap->aa_mountpoint);
			(void) snprintf(symname_mp, sizeof (symname_mp),
			    "/%s/%s", aap->aa_media, aap->aa_name);
		} else {
			/* /cdrom/<name>/slice -> original mountpoint */
			(void) snprintf(part_dir, sizeof (part_dir),
			    "/%s/%s", aap->aa_media, aap->aa_name);
			(void) snprintf(symcontents_mp, sizeof (symcontents_mp),
			    "%s", aap->aa_mountpoint);
			(void) snprintf(symname_mp, sizeof (symname_mp),
			    "/%s/%s/%s", aap->aa_media, aap->aa_name,
			    aap->aa_partname);

		}
		/* /cdrom/cdrom0 -> ./<name> */
		(void) snprintf(symcontents, sizeof (symcontents),
		    "./%s", aap->aa_name);
		(void) snprintf(symname, sizeof (symname),
		    "/%s/%s", aap->aa_media, aap->aa_symdev);
	} else {
		if (!is_partition) {
			/* /cdrom/<user>/<name> -> original mountpoint */
			(void) snprintf(symcontents_mp, sizeof (symcontents_mp),
			    "%s", aap->aa_mountpoint);
			(void) snprintf(symname_mp, sizeof (symname_mp),
			    "%s/%s/%s", mnt_zoneroot, aap->aa_media,
			    aap->aa_symdev);
		} else {
			/* /cdrom/<user>/<name>/slice -> original mountpoint */
			(void) snprintf(symcontents_mp, sizeof (symcontents_mp),
			    "%s", aap->aa_mountpoint);
			(void) snprintf(symname_mp, sizeof (symname_mp),
			    "%s/%s/%s", mnt_zoneroot, aap->aa_media,
			    aap->aa_symdev, aap->aa_partname);
		}

		/* /cdrom/<user>/cdrom0 -> ./<user>/<name> */
		(void) snprintf(symcontents, sizeof (symcontents),
		    ".%s/%s", mnt_userdir, aap->aa_name);
		(void) snprintf(symname, sizeof (symname), "%s/%s/%s",
		    mnt_zoneroot, aap->aa_media, aap->aa_symdev);
	}

	(void) unlink(symname);
	(void) unlink(symname_mp);
	if (is_partition) {
		(void) rmdir(part_dir);
	}

	if ((aap->aa_action == INSERT) || (aap->aa_action == REMOUNT)) {
		(void) mkdir(aap->aa_media, 0755);
		if (is_partition) {
			(void) mkdir(part_dir, 0755);
		}
		(void) symlink(symcontents_mp, symname_mp);
		(void) symlink(symcontents, symname);
	}
}


static int
vold_action(struct action_arg *aap)
{
	action_t	action;
	int		result;
	int		do_notify = FALSE;
	action_t	notify_act = EJECT;
	struct action_arg *aa[2];
	struct action_arg a1;

	dprintf("%s[%d]: entering action()\n", __FILE__, __LINE__);

	/*
	 * on Trusted Extensions, actions are executed in the user's zone
	 */
	if (mnt_zoneid > GLOBAL_ZONEID) {
		pid_t	pid;
		int	status;
		int	ifx;
		int	tmpl_fd;
		int	err = 0;

		tmpl_fd = open64(CTFS_ROOT "/process/template",
		    O_RDWR);
		if (tmpl_fd == -1)
			return (1);

		/*
		 * Deliver no events, don't inherit,
		 * and allow it to be orphaned.
		 */
		err |= ct_tmpl_set_critical(tmpl_fd, 0);
		err |= ct_tmpl_set_informative(tmpl_fd, 0);
		err |= ct_pr_tmpl_set_fatal(tmpl_fd,
		    CT_PR_EV_HWERR);
		err |= ct_pr_tmpl_set_param(tmpl_fd,
		    CT_PR_PGRPONLY |
		    CT_PR_REGENT);
		if (err || ct_tmpl_activate(tmpl_fd)) {
			(void) close(tmpl_fd);
			return (1);
		}
		switch (pid = fork1()) {
		case 0:
			(void) ct_tmpl_clear(tmpl_fd);
			for (ifx = 0; ifx < _NFILE; ifx++)
				(void) close(ifx);

			if (zone_enter(mnt_zoneid) == -1)
				_exit(0);

			/* entered zone, proceed to action */
			break;
		case -1:
			dprintf("fork1 failed \n ");
			return (1);
		default :
			(void) ct_tmpl_clear(tmpl_fd);
			(void) close(tmpl_fd);
			if (waitpid(pid, &status, 0) < 0) {
				dprintf("%s(%ld): waitpid() "
				    "failed (errno %d) \n",
				    prog_name, prog_pid, errno);
				return (1);
			}
		}
	}

	/* only support one action at a time XXX */
	a1.aa_path = NULL;
	aa[0] = aap;
	aa[1] = &a1;

	action = aa[0]->aa_action;

	if (action == CLEAR_MOUNTS) {
		/*
		 * Remove the notifications files, but don't
		 * notify the client.  The "clear_mounts" action
		 * simply clears all existing mounts of a medium's
		 * partitions after a medium has been repartitioned.
		 * Then vold builds a new file system that reflects
		 * the medium's new partition structure and mounts
		 * the new partitions by calling rmmount, and therefore
		 * action(), with the VOLUME_ACTION environment variable
		 * set to "remount".
		 */
		result = remove_notify_files(aa);
		result = TRUE;
	} else if (action == EJECT) {
		result = remove_notify_files(aa);
		if (result == TRUE) {
			do_notify = TRUE;
			notify_act = EJECT;
		}
	} else if (action = INSERT) {
		result = create_notify_files(aa);
		if (result == TRUE) {
			do_notify = TRUE;
			notify_act = INSERT;
		}
	} else if (action == REMOUNT) {
		result = create_notify_files(aa);
		if (result == TRUE) {
			do_notify = TRUE;
			notify_act = REMOUNT;
		}
	} else if (action == UNMOUNT) {
		result = remove_notify_files(aa);
		if (result == TRUE) {
			do_notify = TRUE;
			notify_act = UNMOUNT;
		}
	} else {
		dprintf("%s[%d]: action(): invalid action: %s\n",
		    __FILE__, __LINE__, action);
		result = FALSE;
	}

	if (result == TRUE) {
		result = notify_clients(notify_act, do_notify);
	}

	dprintf("%s[%d]: leaving action(), result = %s\n",
	    __FILE__, __LINE__, result_strings[result]);

	if (mnt_zoneid > GLOBAL_ZONEID) {
		/* exit forked local zone process */
		_exit(0);
	}

	if (result == TRUE) {
		/*
		 * File Manager is running. return 0.
		 * see man page rmmount.conf(4).
		 */
		return (0);
	} else {
		return (1);
	}
}


/*
 * Returns NULL if a medium or partition is mountable
 * and a string stating the reason the medium or partition
 * can't be mounted if the medium or partition isn't mountable.
 *
 * If the volume_name of the medium or partition is one of the
 * following, the medium or partition isn't mountable.
 *
 * unlabeled_<media_type>
 * unknown_format
 * password_protected
 */
/* ARGSUSED */
static char *
not_mountable(struct action_arg *aa)
{
	return (NULL);
}

static int
create_notify_files(struct action_arg **aa)
{
	int	ai;
	char	*fstype;
	char	*mount_point;
	char	notify_file[64];
	char	*raw_partitionp;
	char	*reason; /* Why the medium wasn't mounted */
	int	result;
	char	*symdev;

	dprintf("%s[%d]: entering create_notify_files()\n", __FILE__, __LINE__);

	ai = 0;
	result = FALSE;
	symdev = aa[ai]->aa_symdev;
	while ((aa[ai] != NULL) && (aa[ai]->aa_path != NULL)) {
		if (aa[ai]->aa_mountpoint != NULL) {
			if (aa[ai]->aa_type) {
				fstype = aa[ai]->aa_type;
			} else {
				fstype = "unknown";
			}
			mount_point = aa[ai]->aa_mountpoint;
			if (aa[ai]->aa_partname != NULL) {
				/*
				 * Is aa_partname ever NULL?
				 * When time permits, check.
				 * If it is, the action taken
				 * in the else clause could produce
				 * file name conflicts.
				 */
				sprintf(notify_file, "%s-%s", symdev,
				    aa[ai]->aa_partname);
			} else {
				sprintf(notify_file, "%s-0", symdev);
			}
			reason = NULL;
		} else {
			/*
			 * The partition isn't mounted.
			 */
			fstype = "none";
			mount_point = "none";
			reason = not_mountable(aa[ai]);
			if (reason != NULL) {
				sprintf(notify_file, "%s-0", symdev);
			} else {
				/*
				 * Either the partition is a backup slice, or
				 * rmmount tried to mount the partition, but
				 * idenf_fs couldn't identify the file system
				 * type; that can occur when rmmount is
				 * trying to mount all the slices in a Solaris
				 * VTOC, and one or more partitions don't have
				 * file systems in them.
				 */
				if (aa[0]->aa_partname != NULL) {
					/*
					 * Is aa_partname ever NULL?
					 * When time permits, check.
					 * If it is, the action taken
					 * in the else clause could produce
					 * file name conflicts.
					 */
					sprintf(notify_file, "%s-%s", symdev,
					    aa[0]->aa_partname);
				} else {
					sprintf(notify_file, "%s-0", symdev);
				}
				if ((aa[0]->aa_type != NULL) &&
				    (strcmp(aa[0]->aa_type, "backup_slice")
				    == 0)) {
					reason = "backup_slice";
				} else {
					reason = "unformatted_media";
				}
				/*
				 * "unformatted_media" should be
				 * changed to "unformmated_medium" for
				 * grammatical correctness, but
				 * "unformatted_media" is now specified
				 * in the interface to filemgr, so the
				 * change can't be made without the
				 * approval of the CDE group.
				 */
			}
		}
		raw_partitionp = aa[0]->aa_rawpath;
		result = create_one_notify_file(fstype,
		    mount_point,
		    notify_file,
		    raw_partitionp,
		    reason,
		    symdev);
		ai++;
	}
	dprintf("%s[%d]: leaving create_notify_files(), result = %s\n",
	    __FILE__, __LINE__, result_strings[result]);
	return (result);
}

static int
create_one_notify_file(char *fstype,
	char *mount_point,
	char *notify_file,
	char *raw_partitionp,
	char *reason,
	char *symdev)
{
	/*
	 * For a mounted partition, create a notification file
	 * indicating the mount point,  the raw device pathname
	 * of the partition, and the partition's file system
	 * type.  For an unmounted partition, create a
	 * notification file containing the reason that the
	 * partition wasn't mounted and the raw device pathname
	 * of the partition.
	 *
	 * Create the file as root in a world-writable
	 * directory that resides in a world-writable directory.
	 *
	 * Handle two possible race conditions that could
	 * allow security breaches.
	 */

	int	current_working_dir_fd;
	int	file_descriptor;
	FILE	*filep;
	int	result;

	dprintf("%s[%d]:Entering create_one_notify_file()\n",
	    __FILE__, __LINE__);
	dprintf("\tcreate_one_notify_file(): fstype = %s\n", fstype);
	dprintf("\tcreate_one_notify_file(): mount_point = %s\n", mount_point);
	dprintf("\tcreate_one_notify_file(): notify_file = %s\n", notify_file);
	dprintf("\tcreate_one_notify_file(): raw_partitionp = %s\n",
	    raw_partitionp);
	if (reason != NULL) {
		dprintf("\tcreate_one_notify_file(): reason = %s\n", reason);
	} else {
		dprintf("\tcreate_one_notify_file(): reason = NULL\n");
	}
	dprintf("\tcreate_one_notify_file(): symdev = %s\n", symdev);

	result = TRUE;
	/*
	 * Handle Race Condition One:
	 *
	 *   If NOTIFY_DIR exists, make sure it is not a symlink.
	 *   if it is, remove it and try to create it.  Check
	 *   again to make sure NOTIFY_DIR isn't a symlink.
	 *   If it is, remove it and return without creating
	 *   a notification file.  The condition can only occur if
	 *   someone is trying to break into the system by running
	 *   a program that repeatedly creates NOTIFY_DIR as a
	 *   symlink.  If NOTIFY_DIR exists and isn't a symlink,
	 *   change the working directory to NOTIFY_DIR.
	 */
	current_working_dir_fd = pushdir(NOTIFY_DIR);
	if (current_working_dir_fd < 0) {
		(void) makepath(NOTIFY_DIR, 0777);
		current_working_dir_fd = pushdir(NOTIFY_DIR);
		if (current_working_dir_fd < 0) {
			result = FALSE;
		}
	}
	/*
	 * Handle Race Condition Two:
	 *
	 * Create the notification file in NOTIFY_DIR.
	 * Remove any files with the same name that may already be
	 * there, using remove(), as it safely removes directories.
	 * Then open the file O_CREAT|O_EXCL, which doesn't follow
	 * symlinks and requires that the file not exist already,
	 * so the new file actually resides in the current working
	 * directory.  Create the file with access mode 644, which
	 * renders it unusable by anyone trying to break into the
	 * system.
	 */
	if (result == TRUE) {
		/*
		 * The current working directory is now NOTIFY_DIR.
		 */
		(void) remove(notify_file);
		file_descriptor =
		    open(notify_file, O_CREAT|O_EXCL|O_WRONLY, 0644);
		if (file_descriptor < 0) {
			dprintf("%s[%d]: can't create %s/%s; %m\n",
			    __FILE__, __LINE__, NOTIFY_DIR, notify_file);
			result = FALSE;
		} else {
			filep = fdopen(file_descriptor, "w");
			if (filep != NULL) {
				if (reason == NULL) {
					(void) fprintf(filep, "%s %s %s",
					    mount_point,
					    raw_partitionp,
					    fstype);
					(void) fclose(filep);
				dprintf("%s[%d]: Just wrote %s %s %s to %s\n",
				    __FILE__,
				    __LINE__,
				    mount_point,
				    raw_partitionp,
				    fstype,
				    notify_file);
				} else {
					(void) fprintf(filep, "%s %s",
					    reason, raw_partitionp);
					(void) fclose(filep);
				dprintf("%s[%d]: Just wrote %s %s to %s\n",
				    __FILE__,
				    __LINE__,
				    reason,
				    raw_partitionp,
				    notify_file);
				}
			} else {
				dprintf("%s[%d]: can't write %s/%s; %m\n",
				    __FILE__, __LINE__,
				    NOTIFY_DIR, notify_file);
				(void) close(file_descriptor);
				result = FALSE;
			}
		}
		popdir(current_working_dir_fd);
	}
	dprintf("%s[%d]: leaving create_one_notify_file, result = %s\n",
	    __FILE__, __LINE__, result_strings[result]);
	return (result);
}

static boolean_t
notify_clients(action_t action, int do_notify)
{
	/*
	 * Notify interested applications of changes in the state
	 * of removable media.  Interested applications are those
	 * that create a named pipe in NOTIFY_DIR with a name that
	 * begins with "notify".  Open the pipe and write a
	 * character through it that indicates the type of state
	 * change = 'e' for ejections, 'i' for insertions, 'r'
	 * for remounts of the file systems on repartitioned media,
	 * and 'u' for unmounts of file systems.
	 */

	int		current_working_dir_fd;
	DIR		*dirp;
	struct dirent	*dir_entryp;
	size_t		len;
	int		fd;
	char		namebuf[MAXPATHLEN];
	char		notify_character;
	void		(*old_signal_handler)();
	int		result;
	struct stat	sb;

	dprintf("%s[%d]: entering notify_clients()\n", __FILE__, __LINE__);

	result = TRUE;
	/*
	 * Use relative pathnames after changing the
	 * working directory to the notification directory.
	 * Check to make sure that each "notify" file is a
	 * named pipe to make sure that it hasn't changed
	 * its file type, which could mean that someone is
	 * trying to use "notify" files to break into the
	 * system.
	 */
	if ((current_working_dir_fd = pushdir(NOTIFY_DIR)) < 0) {
		result = FALSE;
	}
	if (result == TRUE) {
		dirp = opendir(".");
		if (dirp == NULL) {
			dprintf("%s[%d]:opendir failed on '.'; %m\n",
			    __FILE__, __LINE__);
			popdir(current_working_dir_fd);
			result = FALSE;
		}
	}
	if (result == TRUE) {
		/*
		 * Read through the directory and write a notify
		 * character to all files whose names start with "notify".
		 */
		result = FALSE;
		old_signal_handler = signal(SIGPIPE, SIG_IGN);
		len = strlen(NOTIFY_NAME);
		while (dir_entryp = readdir(dirp)) {
			if (strncmp(dir_entryp->d_name, NOTIFY_NAME, len)
			    != 0) {
				continue;
			}
			result = TRUE;
			if (do_notify != TRUE) {
				continue;
			}
			(void) sprintf(namebuf, "%s/%s",
			    NOTIFY_DIR, dir_entryp->d_name);
			if ((fd = open(namebuf, O_WRONLY|O_NDELAY)) < 0) {
				dprintf("%s[%d]: open failed for %s; %m\n",
				    __FILE__, __LINE__, namebuf);
				continue;
			}
			/*
			 * Check to be sure that the entry is a named pipe.
			 * That closes a small security hole that could
			 * enable unauthorized access to the system root.
			 */
			if ((fstat(fd, &sb) < 0) || (!S_ISFIFO(sb.st_mode))) {
				dprintf("%s[%d]: %s isn't a named pipe\n",
				    __FILE__, __LINE__, namebuf);

				(void) close(fd);
				continue;
			}
			notify_character = notify_characters[action];
			if (write(fd, &notify_character, 1) < 0) {
				dprintf("%s[%d]: write failed for %s; %m\n",
				    __FILE__, __LINE__, namebuf);
				(void) close(fd);
				continue;
			}
			(void) close(fd);
		}
		(void) closedir(dirp);
		(void) signal(SIGPIPE, old_signal_handler);
		popdir(current_working_dir_fd);
	}
	dprintf("%s[%d]: leaving notify_clients(), result = %s\n",
	    __FILE__, __LINE__, result_strings[result]);
	return (result);
}

static void
popdir(int fd)
{
	/*
	 * Change the current working directory to the directory
	 * specified by fd and close the fd.  Exit the program
	 * on failure.
	 */
	if (fchdir(fd) < 0) {
		dprintf("%s[%d]: popdir() failed\n", __FILE__, __LINE__);
		exit(1);
	}
	(void) close(fd);
}

static int
pushdir(const char *dir)
{
	/*
	 * Change the current working directory to dir and
	 * return a file descriptor for the old working
	 * directory.
	 *
	 * Exception handling:
	 *
	 * If dir doesn't exist, leave the current working
	 * directory the same and return -1.
	 *
	 * If dir isn't a directory, remove it, leave the
	 * current working directory the same, and return -1.
	 *
	 * If open() fails on the current working directory
	 * or the chdir operation fails on dir, leave the
	 * current working directory the same and return -1.
	 */

	int		current_working_dir_fd;
	struct stat	stat_buf;

	if (lstat(dir, &stat_buf) < 0) {
		dprintf("%s[%d]: push_dir_and_check(): %s does not exist\n",
		    __FILE__, __LINE__, dir);
		return (-1);
	}

	if (!(S_ISDIR(stat_buf.st_mode))) {
		dprintf("%s[%d]: push_dir_and_check(): %s not a directory.\n",
		    __FILE__, __LINE__, dir);
		(void) remove(dir);
		return (-1);
	}
	if ((current_working_dir_fd = open(".", O_RDONLY)) < 0) {
		dprintf("%s[%d]: push_dir_and_check(): can't open %s.\n",
		    __FILE__, __LINE__, dir);
		return (-1);
	}
	if (chdir(dir) < 0) {
		(void) close(current_working_dir_fd);
		dprintf("%s[%d]: push_dir_and_check(): can't chdir() to %s.\n",
		    __FILE__, __LINE__, dir);
		return (-1);
	}
	return (current_working_dir_fd);
}

static boolean_t
remove_notify_files(struct action_arg **aa)
{
	int	ai;
	int	current_working_dir_fd;
	char	notify_file[64];
	int	result;
	char	*symdev;

	dprintf("%s[%d]: entering remove_notify_files()\n", __FILE__, __LINE__);

	ai = 0;
	result = TRUE;
	symdev = aa[ai]->aa_symdev;
	while ((result == TRUE) &&
	    (aa[ai] != NULL) &&
	    (aa[ai]->aa_path != NULL)) {

		if (not_mountable(aa[ai])) {
			sprintf(notify_file, "%s-0", symdev);
		} else if (aa[ai]->aa_partname != NULL) {
			/*
			 * Is aa_partname ever NULL?
			 * When time permits, check.
			 * If it is, the action taken
			 * in the else clause could produce
			 * file name conflicts.
			 */
			sprintf(notify_file, "%s-%s",
			    symdev, aa[0]->aa_partname);
		} else {
			sprintf(notify_file, "%s-0", symdev);
		}

		current_working_dir_fd = pushdir(NOTIFY_DIR);
		if (current_working_dir_fd < 0) {
			result = FALSE;
		}
		if ((result == TRUE) && (remove(notify_file) < 0)) {
			dprintf("%s[%d]: remove %s/%s; %m\n",
			    __FILE__, __LINE__, NOTIFY_DIR, notify_file);
			result = FALSE;
		}
		if (current_working_dir_fd != -1) {
			popdir(current_working_dir_fd);
		}
		ai++;
	}
	dprintf("%s[%d]: leaving remove_notify_files(), result = %s\n",
	    __FILE__, __LINE__, result_strings[result]);

	return (result);
}