xref: /titanic_51/usr/src/uts/common/syscall/readlink.c (revision fb9f9b975cb9214fec5dab37d461199adab9b964)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <sys/param.h>
38 #include <sys/isa_defs.h>
39 #include <sys/types.h>
40 #include <sys/sysmacros.h>
41 #include <sys/cred.h>
42 #include <sys/systm.h>
43 #include <sys/errno.h>
44 #include <sys/pathname.h>
45 #include <sys/vnode.h>
46 #include <sys/file.h>
47 #include <sys/uio.h>
48 #include <sys/debug.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 
63 	if ((cnt = (ssize_t)count) < 0)
64 		return (set_errno(EINVAL));
65 
66 lookup:
67 	if (error = lookupname(name, UIO_USERSPACE, NO_FOLLOW, NULLVPP, &vp)) {
68 		if (error == ESTALE)
69 			goto lookup;
70 		return (set_errno(error));
71 	}
72 
73 	if (vp->v_type != VLNK) {
74 		/*
75 		 * Ask the underlying filesystem if it wants this
76 		 * object to look like a symlink at user-level.
77 		 */
78 		vattr.va_mask = AT_TYPE;
79 		error = VOP_GETATTR(vp, &vattr, 0, CRED());
80 		if (error || vattr.va_type != VLNK) {
81 			VN_RELE(vp);
82 			if (error == ESTALE)
83 				goto lookup;
84 			return (set_errno(EINVAL));
85 		}
86 	}
87 	aiov.iov_base = buf;
88 	aiov.iov_len = cnt;
89 	auio.uio_iov = &aiov;
90 	auio.uio_iovcnt = 1;
91 	auio.uio_loffset = 0;
92 	auio.uio_segflg = UIO_USERSPACE;
93 	auio.uio_extflg = UIO_COPY_CACHED;
94 	auio.uio_resid = cnt;
95 	error = VOP_READLINK(vp, &auio, CRED());
96 	VN_RELE(vp);
97 	if (error) {
98 		if (error == ESTALE)
99 			goto lookup;
100 		return (set_errno(error));
101 	}
102 	return ((ssize_t)(cnt - auio.uio_resid));
103 }
104 
105 #ifdef _SYSCALL32_IMPL
106 /*
107  * readlink32() intentionally returns a ssize_t rather than ssize32_t;
108  * see the comments above read32 for details.
109  */
110 
111 ssize_t
112 readlink32(caddr32_t name, caddr32_t buf, size32_t count)
113 {
114 	return ((ssize32_t)readlink((char *)(uintptr_t)name,
115 	    (char *)(uintptr_t)buf, (ssize32_t)count));
116 }
117 
118 #endif	/* _SYSCALL32_IMPL */
119