xref: /illumos-gate/usr/src/uts/common/syscall/readlink.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
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/ioreq.h>
49 #include <sys/debug.h>
50 
51 /*
52  * Read the contents of a symbolic link.
53  */
54 ssize_t
55 readlink(char *name, char *buf, size_t count)
56 {
57 	vnode_t *vp;
58 	struct iovec aiov;
59 	struct uio auio;
60 	int error;
61 	struct vattr vattr;
62 	ssize_t cnt;
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)
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());
81 		if (error || vattr.va_type != VLNK) {
82 			VN_RELE(vp);
83 			if (error == ESTALE)
84 				goto lookup;
85 			return (set_errno(EINVAL));
86 		}
87 	}
88 	aiov.iov_base = buf;
89 	aiov.iov_len = cnt;
90 	auio.uio_iov = &aiov;
91 	auio.uio_iovcnt = 1;
92 	auio.uio_loffset = 0;
93 	auio.uio_segflg = UIO_USERSPACE;
94 	auio.uio_extflg = UIO_COPY_CACHED;
95 	auio.uio_resid = cnt;
96 	error = VOP_READLINK(vp, &auio, CRED());
97 	VN_RELE(vp);
98 	if (error) {
99 		if (error == ESTALE)
100 			goto lookup;
101 		return (set_errno(error));
102 	}
103 	return ((ssize_t)(cnt - auio.uio_resid));
104 }
105 
106 #ifdef _SYSCALL32_IMPL
107 /*
108  * readlink32() intentionally returns a ssize_t rather than ssize32_t;
109  * see the comments above read32 for details.
110  */
111 
112 ssize_t
113 readlink32(caddr32_t name, caddr32_t buf, size32_t count)
114 {
115 	return ((ssize32_t)readlink((char *)(uintptr_t)name,
116 	    (char *)(uintptr_t)buf, (ssize32_t)count));
117 }
118 
119 #endif	/* _SYSCALL32_IMPL */
120