xref: /titanic_41/usr/src/lib/libc/port/gen/fattach.c (revision 1c42de6d020629af774dd9e9fc81be3f3ed9398e)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 /*
34  * Attach a STREAMS or door based file descriptor to an object in the file
35  * system name space.
36  */
37 #pragma weak fattach = _fattach
38 #include "synonyms.h"
39 #include <sys/types.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stropts.h>
43 #include <sys/vnode.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