xref: /titanic_41/usr/src/uts/common/syscall/readlink.c (revision 2654012f83cec5dc15b61dfe3e4a4915f186e7a6)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*
30  * Portions of this source code were derived from Berkeley 4.3 BSD
31  * under license from the Regents of the University of California.
32  */
33 
34 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 
36 #include <sys/param.h>
37 #include <sys/isa_defs.h>
38 #include <sys/types.h>
39 #include <sys/sysmacros.h>
40 #include <sys/cred.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/pathname.h>
44 #include <sys/vnode.h>
45 #include <sys/file.h>
46 #include <sys/uio.h>
47 #include <sys/debug.h>
48 #include <fs/fs_subr.h>
49 
50 /*
51  * Read the contents of a symbolic link.
52  */
53 ssize_t
54 readlink(char *name, char *buf, size_t count)
55 {
56 	vnode_t *vp;
57 	struct iovec aiov;
58 	struct uio auio;
59 	int error;
60 	struct vattr vattr;
61 	ssize_t cnt;
62 	int estale_retry = 0;
63 
64 	if ((cnt = (ssize_t)count) < 0)
65 		return (set_errno(EINVAL));
66 
67 lookup:
68 	if (error = lookupname(name, UIO_USERSPACE, NO_FOLLOW, NULLVPP, &vp)) {
69 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
70 			goto lookup;
71 		return (set_errno(error));
72 	}
73 
74 	if (vp->v_type != VLNK) {
75 		/*
76 		 * Ask the underlying filesystem if it wants this
77 		 * object to look like a symlink at user-level.
78 		 */
79 		vattr.va_mask = AT_TYPE;
80 		error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL);
81 		if (error || vattr.va_type != VLNK) {
82 			VN_RELE(vp);
83 			if ((error == ESTALE) &&
84 			    fs_need_estale_retry(estale_retry++))
85 				goto lookup;
86 			return (set_errno(EINVAL));
87 		}
88 	}
89 	aiov.iov_base = buf;
90 	aiov.iov_len = cnt;
91 	auio.uio_iov = &aiov;
92 	auio.uio_iovcnt = 1;
93 	auio.uio_loffset = 0;
94 	auio.uio_segflg = UIO_USERSPACE;
95 	auio.uio_extflg = UIO_COPY_CACHED;
96 	auio.uio_resid = cnt;
97 	error = VOP_READLINK(vp, &auio, CRED(), NULL);
98 	VN_RELE(vp);
99 	if (error) {
100 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
101 			goto lookup;
102 		return (set_errno(error));
103 	}
104 	return ((ssize_t)(cnt - auio.uio_resid));
105 }
106 
107 #ifdef _SYSCALL32_IMPL
108 /*
109  * readlink32() intentionally returns a ssize_t rather than ssize32_t;
110  * see the comments above read32 for details.
111  */
112 
113 ssize_t
114 readlink32(caddr32_t name, caddr32_t buf, size32_t count)
115 {
116 	return ((ssize32_t)readlink((char *)(uintptr_t)name,
117 	    (char *)(uintptr_t)buf, (ssize32_t)count));
118 }
119 
120 #endif	/* _SYSCALL32_IMPL */
121