xref: /titanic_44/usr/src/lib/libc/port/gen/fattach.c (revision 308903890e892f9c21ee582e3a52fdd67e52870b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * Attach a STREAMS or door based file descriptor to an object in the file
34  * system name space.
35  */
36 
37 #pragma weak _fattach = fattach
38 
39 #include "lint.h"
40 #include <sys/types.h>
41 #include <errno.h>
42 #include <stdio.h>
43 #include <stropts.h>
44 #include <sys/door.h>
45 #include <sys/fs/namenode.h>
46 #include <sys/mount.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include "libc.h"
50 
51 int
52 fattach(int fildes, const char *path)
53 {
54 	struct namefd  namefdp;
55 	struct door_info dinfo;
56 	int	s;
57 	char buf[MAXPATHLEN];
58 
59 	/* Only STREAMS and doors allowed to be mounted */
60 	if ((s = isastream(fildes)) == 1 || __door_info(fildes, &dinfo) == 0) {
61 		namefdp.fd = fildes;
62 		if (path == NULL || *path == '\0') {
63 			errno = ENOENT;
64 			return (-1);
65 		} else if (*path != '/') {
66 			/*
67 			 * The mount point must be an absolute path.
68 			 */
69 			if (getcwd(buf, sizeof (buf)) == NULL) {
70 				/* errno already set */
71 				return (-1);
72 			}
73 			/*
74 			 * The kernel will truncate the path if it would have
75 			 * turned into something more than MAXPATHLEN bytes.
76 			 * So we do the same here.
77 			 */
78 			if (strlcat(buf, "/",  sizeof (buf)) >= sizeof (buf) ||
79 			    strlcat(buf, path, sizeof (buf)) >= sizeof (buf)) {
80 				errno = ENAMETOOLONG;
81 				return (-1);
82 			}
83 			path = buf;
84 		}
85 		return (mount((char *)NULL, path, MS_DATA|MS_NOMNTTAB,
86 		    (const char *)"namefs", (char *)&namefdp,
87 		    sizeof (struct namefd), NULL, 0));
88 	} else if (s == 0) {
89 		/* Not a STREAM */
90 		errno = EINVAL;
91 		return (-1);
92 	} else {
93 		/* errno already set */
94 		return (-1);
95 	}
96 }
97