1 /*
2 * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "unistd.h"
32 #include "errno.h"
33 #include "vnode.h"
34
do_readlink(int dirfd,const char * path,char * buf,size_t bufsize)35 ssize_t do_readlink(int dirfd, const char *path, char *buf, size_t bufsize)
36 {
37 struct Vnode *vnode = NULL;
38 char *fullpath = NULL;
39 ssize_t ret;
40
41 if (!path)
42 {
43 ret = -EFAULT;
44 goto errout;
45 }
46
47 if (*path == '\0' || buf == NULL || bufsize < 0)
48 {
49 ret = -EINVAL;
50 goto errout;
51 }
52
53 ret = vfs_normalize_pathat(dirfd, path, &fullpath);
54 if (ret < 0)
55 {
56 goto errout;
57 }
58
59 VnodeHold();
60 ret = VnodeLookup(fullpath, &vnode, 0);
61 if (ret < 0)
62 {
63 goto errout_with_vnode;
64 }
65 #ifdef LOSCFG_PROC_PROCESS_DIR
66 if (vnode->type != VNODE_TYPE_LNK && vnode->type != VNODE_TYPE_VIR_LNK)
67 #else
68 if (vnode->type != VNODE_TYPE_LNK)
69 #endif
70 {
71 ret = -EINVAL;
72 goto errout_with_vnode;
73 }
74
75 if (!vnode->vop || !vnode->vop->Readlink)
76 {
77 ret = -ENOSYS;
78 goto errout_with_vnode;
79 }
80
81 ret = vnode->vop->Readlink(vnode, buf, bufsize);
82 if (ret < 0)
83 {
84 goto errout_with_vnode;
85 }
86
87 VnodeDrop();
88
89 free(fullpath);
90
91 return ret;
92
93 errout_with_vnode:
94 VnodeDrop();
95 free(fullpath);
96 errout:
97 set_errno(-ret);
98 return VFS_ERROR;
99 }
100
readlink(const char * pathname,char * buf,size_t bufsize)101 ssize_t readlink(const char *pathname, char *buf, size_t bufsize)
102 {
103 return do_readlink(AT_FDCWD, pathname, buf, bufsize);
104 }
105
readlinkat(int dirfd,const char * pathname,char * buf,size_t bufsize)106 ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsize)
107 {
108 return do_readlink(dirfd, pathname, buf, bufsize);
109 }
110