xref: /illumos-gate/usr/src/lib/libc/port/gen/fattach.c (revision 1da57d55)
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
5458cf4d6Sek  * Common Development and Distribution License (the "License").
6458cf4d6Sek  * 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 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Attach a STREAMS or door based file descriptor to an object in the file
327c478bd9Sstevel@tonic-gate  * system name space.
337c478bd9Sstevel@tonic-gate  */
34*7257d1b4Sraf 
35*7257d1b4Sraf #pragma weak _fattach = fattach
36*7257d1b4Sraf 
37*7257d1b4Sraf #include "lint.h"
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <stropts.h>
427c478bd9Sstevel@tonic-gate #include <sys/door.h>
437c478bd9Sstevel@tonic-gate #include <sys/fs/namenode.h>
447c478bd9Sstevel@tonic-gate #include <sys/mount.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate #include "libc.h"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate int
fattach(int fildes,const char * path)507c478bd9Sstevel@tonic-gate fattach(int fildes, const char *path)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate 	struct namefd  namefdp;
537c478bd9Sstevel@tonic-gate 	struct door_info dinfo;
547c478bd9Sstevel@tonic-gate 	int	s;
557c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	/* Only STREAMS and doors allowed to be mounted */
587c478bd9Sstevel@tonic-gate 	if ((s = isastream(fildes)) == 1 || __door_info(fildes, &dinfo) == 0) {
597c478bd9Sstevel@tonic-gate 		namefdp.fd = fildes;
607c478bd9Sstevel@tonic-gate 		if (path == NULL || *path == '\0') {
617c478bd9Sstevel@tonic-gate 			errno = ENOENT;
627c478bd9Sstevel@tonic-gate 			return (-1);
637c478bd9Sstevel@tonic-gate 		} else if (*path != '/') {
647c478bd9Sstevel@tonic-gate 			/*
657c478bd9Sstevel@tonic-gate 			 * The mount point must be an absolute path.
667c478bd9Sstevel@tonic-gate 			 */
677c478bd9Sstevel@tonic-gate 			if (getcwd(buf, sizeof (buf)) == NULL) {
687c478bd9Sstevel@tonic-gate 				/* errno already set */
697c478bd9Sstevel@tonic-gate 				return (-1);
707c478bd9Sstevel@tonic-gate 			}
717c478bd9Sstevel@tonic-gate 			/*
727c478bd9Sstevel@tonic-gate 			 * The kernel will truncate the path if it would have
737c478bd9Sstevel@tonic-gate 			 * turned into something more than MAXPATHLEN bytes.
747c478bd9Sstevel@tonic-gate 			 * So we do the same here.
757c478bd9Sstevel@tonic-gate 			 */
767c478bd9Sstevel@tonic-gate 			if (strlcat(buf, "/",  sizeof (buf)) >= sizeof (buf) ||
777c478bd9Sstevel@tonic-gate 			    strlcat(buf, path, sizeof (buf)) >= sizeof (buf)) {
787c478bd9Sstevel@tonic-gate 				errno = ENAMETOOLONG;
797c478bd9Sstevel@tonic-gate 				return (-1);
807c478bd9Sstevel@tonic-gate 			}
817c478bd9Sstevel@tonic-gate 			path = buf;
827c478bd9Sstevel@tonic-gate 		}
837c478bd9Sstevel@tonic-gate 		return (mount((char *)NULL, path, MS_DATA|MS_NOMNTTAB,
847c478bd9Sstevel@tonic-gate 		    (const char *)"namefs", (char *)&namefdp,
857c478bd9Sstevel@tonic-gate 		    sizeof (struct namefd), NULL, 0));
867c478bd9Sstevel@tonic-gate 	} else if (s == 0) {
877c478bd9Sstevel@tonic-gate 		/* Not a STREAM */
887c478bd9Sstevel@tonic-gate 		errno = EINVAL;
897c478bd9Sstevel@tonic-gate 		return (-1);
907c478bd9Sstevel@tonic-gate 	} else {
917c478bd9Sstevel@tonic-gate 		/* errno already set */
927c478bd9Sstevel@tonic-gate 		return (-1);
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate }
95