xref: /illumos-gate/usr/src/lib/libc/port/gen/fattach.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
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 /*
31  * Attach a STREAMS or door based file descriptor to an object in the file
32  * system name space.
33  */
34 
35 #pragma weak _fattach = fattach
36 
37 #include "lint.h"
38 #include <sys/types.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stropts.h>
42 #include <sys/door.h>
43 #include <sys/fs/namenode.h>
44 #include <sys/mount.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include "libc.h"
48 
49 int
50 fattach(int fildes, const char *path)
51 {
52 	struct namefd  namefdp;
53 	struct door_info dinfo;
54 	int	s;
55 	char buf[MAXPATHLEN];
56 
57 	/* Only STREAMS and doors allowed to be mounted */
58 	if ((s = isastream(fildes)) == 1 || __door_info(fildes, &dinfo) == 0) {
59 		namefdp.fd = fildes;
60 		if (path == NULL || *path == '\0') {
61 			errno = ENOENT;
62 			return (-1);
63 		} else if (*path != '/') {
64 			/*
65 			 * The mount point must be an absolute path.
66 			 */
67 			if (getcwd(buf, sizeof (buf)) == NULL) {
68 				/* errno already set */
69 				return (-1);
70 			}
71 			/*
72 			 * The kernel will truncate the path if it would have
73 			 * turned into something more than MAXPATHLEN bytes.
74 			 * So we do the same here.
75 			 */
76 			if (strlcat(buf, "/",  sizeof (buf)) >= sizeof (buf) ||
77 			    strlcat(buf, path, sizeof (buf)) >= sizeof (buf)) {
78 				errno = ENAMETOOLONG;
79 				return (-1);
80 			}
81 			path = buf;
82 		}
83 		return (mount((char *)NULL, path, MS_DATA|MS_NOMNTTAB,
84 		    (const char *)"namefs", (char *)&namefdp,
85 		    sizeof (struct namefd), NULL, 0));
86 	} else if (s == 0) {
87 		/* Not a STREAM */
88 		errno = EINVAL;
89 		return (-1);
90 	} else {
91 		/* errno already set */
92 		return (-1);
93 	}
94 }
95