xref: /onnv-gate/usr/src/lib/libc/port/gen/fattach.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55006Sek110237  * Common Development and Distribution License (the "License").
65006Sek110237  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * Attach a STREAMS or door based file descriptor to an object in the file
340Sstevel@tonic-gate  * system name space.
350Sstevel@tonic-gate  */
36*6812Sraf 
37*6812Sraf #pragma weak _fattach = fattach
38*6812Sraf 
39*6812Sraf #include "lint.h"
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <stropts.h>
440Sstevel@tonic-gate #include <sys/door.h>
450Sstevel@tonic-gate #include <sys/fs/namenode.h>
460Sstevel@tonic-gate #include <sys/mount.h>
470Sstevel@tonic-gate #include <unistd.h>
480Sstevel@tonic-gate #include <string.h>
490Sstevel@tonic-gate #include "libc.h"
500Sstevel@tonic-gate 
510Sstevel@tonic-gate int
fattach(int fildes,const char * path)520Sstevel@tonic-gate fattach(int fildes, const char *path)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	struct namefd  namefdp;
550Sstevel@tonic-gate 	struct door_info dinfo;
560Sstevel@tonic-gate 	int	s;
570Sstevel@tonic-gate 	char buf[MAXPATHLEN];
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	/* Only STREAMS and doors allowed to be mounted */
600Sstevel@tonic-gate 	if ((s = isastream(fildes)) == 1 || __door_info(fildes, &dinfo) == 0) {
610Sstevel@tonic-gate 		namefdp.fd = fildes;
620Sstevel@tonic-gate 		if (path == NULL || *path == '\0') {
630Sstevel@tonic-gate 			errno = ENOENT;
640Sstevel@tonic-gate 			return (-1);
650Sstevel@tonic-gate 		} else if (*path != '/') {
660Sstevel@tonic-gate 			/*
670Sstevel@tonic-gate 			 * The mount point must be an absolute path.
680Sstevel@tonic-gate 			 */
690Sstevel@tonic-gate 			if (getcwd(buf, sizeof (buf)) == NULL) {
700Sstevel@tonic-gate 				/* errno already set */
710Sstevel@tonic-gate 				return (-1);
720Sstevel@tonic-gate 			}
730Sstevel@tonic-gate 			/*
740Sstevel@tonic-gate 			 * The kernel will truncate the path if it would have
750Sstevel@tonic-gate 			 * turned into something more than MAXPATHLEN bytes.
760Sstevel@tonic-gate 			 * So we do the same here.
770Sstevel@tonic-gate 			 */
780Sstevel@tonic-gate 			if (strlcat(buf, "/",  sizeof (buf)) >= sizeof (buf) ||
790Sstevel@tonic-gate 			    strlcat(buf, path, sizeof (buf)) >= sizeof (buf)) {
800Sstevel@tonic-gate 				errno = ENAMETOOLONG;
810Sstevel@tonic-gate 				return (-1);
820Sstevel@tonic-gate 			}
830Sstevel@tonic-gate 			path = buf;
840Sstevel@tonic-gate 		}
850Sstevel@tonic-gate 		return (mount((char *)NULL, path, MS_DATA|MS_NOMNTTAB,
860Sstevel@tonic-gate 		    (const char *)"namefs", (char *)&namefdp,
870Sstevel@tonic-gate 		    sizeof (struct namefd), NULL, 0));
880Sstevel@tonic-gate 	} else if (s == 0) {
890Sstevel@tonic-gate 		/* Not a STREAM */
900Sstevel@tonic-gate 		errno = EINVAL;
910Sstevel@tonic-gate 		return (-1);
920Sstevel@tonic-gate 	} else {
930Sstevel@tonic-gate 		/* errno already set */
940Sstevel@tonic-gate 		return (-1);
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate }
97