xref: /original-bsd/usr.sbin/amd/amd/pfs_ops.c (revision b001ed0c)
1 /*
2  * $Id: pfs_ops.c,v 5.2 90/06/23 22:19:53 jsp Rel $
3  *
4  * Copyright (c) 1989 Jan-Simon Pendry
5  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
6  * Copyright (c) 1989 The Regents of the University of California.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Jan-Simon Pendry at Imperial College, London.
11  *
12  * %sccs.include.redist.c%
13  *
14  *	@(#)pfs_ops.c	5.1 (Berkeley) 06/29/90
15  */
16 
17 #include "am.h"
18 
19 #ifdef HAS_PFS
20 
21 /*
22  * Program file system
23  */
24 
25 /*
26  * Execute needs a mount and unmount command.
27  */
28 static int pfs_match(fo)
29 am_opts *fo;
30 {
31 	char *prog;
32 	if (!fo->opt_mount || !fo->opt_unmount) {
33 		plog(XLOG_USER, "program: no mount/unmount specified");
34 		return FALSE;
35 	}
36 	prog = strchr(fo->opt_mount, ' ');
37 	if (fo->fs_mtab = strealloc(fo->fs_mtab, prog ? prog+1 : fo->opt_mount))
38 		return TRUE;
39 	return FALSE;
40 }
41 
42 static int pfs_init(mf)
43 mntfs *mf;
44 {
45 	/*
46 	 * Save unmount command
47 	 */
48 	if (mf->mf_refc == 1) {
49 		mf->mf_private = (voidp) strdup(mf->mf_fo->opt_unmount);
50 		mf->mf_prfree = (void (*) ()) free;
51 	}
52 	return 0;
53 }
54 
55 static int pfs_exec(info)
56 char *info;
57 {
58 	char **xivec;
59 	int error;
60 	/*
61 	 * Split copy of command info string
62 	 */
63 	info = strdup(info);
64 	if (info == 0)
65 		return ENOBUFS;
66 	xivec = strsplit(info, '\'');
67 	/*
68 	 * Put stdout to stderr
69 	 */
70 	(void) fclose(stdout);
71 	(void) dup(fileno(logfp));
72 	if (fileno(logfp) != fileno(stderr)) {
73 		(void) fclose(stderr);
74 		(void) dup(fileno(logfp));
75 	}
76 	/*
77 	 * Try the exec
78 	 */
79 #ifdef DEBUG
80 	Debug(D_FULL) {
81 		char **cp = xivec;
82 		plog(XLOG_DEBUG, "executing (un)mount command...");
83 		while (*cp) {
84 	  		plog(XLOG_DEBUG, "arg[%d] = '%s'", cp-xivec, *cp);
85 			cp++;
86 		}
87 	}
88 #endif /* DEBUG */
89 	if (xivec[0] == 0 || xivec[1] == 0) {
90 		errno = EINVAL;
91 		plog(XLOG_USER, "1st/2nd args missing to (un)mount program");
92 	} else {
93 		(void) execv(xivec[0], xivec+1);
94 	}
95 	/*
96 	 * Save error number
97 	 */
98 	error = errno;
99 	plog(XLOG_ERROR, "exec failed: %m");
100 
101 	/*
102 	 * Free allocate memory
103 	 */
104 	free(info);
105 	free(xivec);
106 	/*
107 	 * Return error
108 	 */
109 	return error;
110 }
111 
112 static int pfs_mount(mp)
113 am_node *mp;
114 {
115 	mntfs *mf = mp->am_mnt;
116 
117 	return pfs_exec(mf->mf_fo->opt_mount);
118 }
119 
120 static int pfs_umount(mp)
121 am_node *mp;
122 {
123 	mntfs *mf = mp->am_mnt;
124 
125 	return pfs_exec((char *) mf->mf_private);
126 }
127 
128 /*
129  * Ops structure
130  */
131 am_ops pfs_ops = {
132 	"program",
133 	pfs_match,
134 	pfs_init,
135 	pfs_mount,
136 	pfs_umount,
137 	efs_lookuppn,
138 	efs_readdir,
139 	0, /* pfs_readlink */
140 	0, /* pfs_mounted */
141 	0, /* pfs_umounted */
142 	find_afs_srvr,
143 	FS_BACKGROUND|FS_AMQINFO
144 };
145 
146 #endif /* HAS_PFS */
147