xref: /titanic_41/usr/src/lib/libc/port/gen/fattach.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5458cf4d6Sek110237  * Common Development and Distribution License (the "License").
6458cf4d6Sek110237  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*7257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
30*7257d1b4Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Attach a STREAMS or door based file descriptor to an object in the file
347c478bd9Sstevel@tonic-gate  * system name space.
357c478bd9Sstevel@tonic-gate  */
36*7257d1b4Sraf 
37*7257d1b4Sraf #pragma weak _fattach = fattach
38*7257d1b4Sraf 
39*7257d1b4Sraf #include "lint.h"
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <errno.h>
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include <stropts.h>
447c478bd9Sstevel@tonic-gate #include <sys/door.h>
457c478bd9Sstevel@tonic-gate #include <sys/fs/namenode.h>
467c478bd9Sstevel@tonic-gate #include <sys/mount.h>
477c478bd9Sstevel@tonic-gate #include <unistd.h>
487c478bd9Sstevel@tonic-gate #include <string.h>
497c478bd9Sstevel@tonic-gate #include "libc.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate int
fattach(int fildes,const char * path)527c478bd9Sstevel@tonic-gate fattach(int fildes, const char *path)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	struct namefd  namefdp;
557c478bd9Sstevel@tonic-gate 	struct door_info dinfo;
567c478bd9Sstevel@tonic-gate 	int	s;
577c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	/* Only STREAMS and doors allowed to be mounted */
607c478bd9Sstevel@tonic-gate 	if ((s = isastream(fildes)) == 1 || __door_info(fildes, &dinfo) == 0) {
617c478bd9Sstevel@tonic-gate 		namefdp.fd = fildes;
627c478bd9Sstevel@tonic-gate 		if (path == NULL || *path == '\0') {
637c478bd9Sstevel@tonic-gate 			errno = ENOENT;
647c478bd9Sstevel@tonic-gate 			return (-1);
657c478bd9Sstevel@tonic-gate 		} else if (*path != '/') {
667c478bd9Sstevel@tonic-gate 			/*
677c478bd9Sstevel@tonic-gate 			 * The mount point must be an absolute path.
687c478bd9Sstevel@tonic-gate 			 */
697c478bd9Sstevel@tonic-gate 			if (getcwd(buf, sizeof (buf)) == NULL) {
707c478bd9Sstevel@tonic-gate 				/* errno already set */
717c478bd9Sstevel@tonic-gate 				return (-1);
727c478bd9Sstevel@tonic-gate 			}
737c478bd9Sstevel@tonic-gate 			/*
747c478bd9Sstevel@tonic-gate 			 * The kernel will truncate the path if it would have
757c478bd9Sstevel@tonic-gate 			 * turned into something more than MAXPATHLEN bytes.
767c478bd9Sstevel@tonic-gate 			 * So we do the same here.
777c478bd9Sstevel@tonic-gate 			 */
787c478bd9Sstevel@tonic-gate 			if (strlcat(buf, "/",  sizeof (buf)) >= sizeof (buf) ||
797c478bd9Sstevel@tonic-gate 			    strlcat(buf, path, sizeof (buf)) >= sizeof (buf)) {
807c478bd9Sstevel@tonic-gate 				errno = ENAMETOOLONG;
817c478bd9Sstevel@tonic-gate 				return (-1);
827c478bd9Sstevel@tonic-gate 			}
837c478bd9Sstevel@tonic-gate 			path = buf;
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 		return (mount((char *)NULL, path, MS_DATA|MS_NOMNTTAB,
867c478bd9Sstevel@tonic-gate 		    (const char *)"namefs", (char *)&namefdp,
877c478bd9Sstevel@tonic-gate 		    sizeof (struct namefd), NULL, 0));
887c478bd9Sstevel@tonic-gate 	} else if (s == 0) {
897c478bd9Sstevel@tonic-gate 		/* Not a STREAM */
907c478bd9Sstevel@tonic-gate 		errno = EINVAL;
917c478bd9Sstevel@tonic-gate 		return (-1);
927c478bd9Sstevel@tonic-gate 	} else {
937c478bd9Sstevel@tonic-gate 		/* errno already set */
947c478bd9Sstevel@tonic-gate 		return (-1);
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate }
97