xref: /freebsd/sys/compat/linux/linux_getcwd.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 /* $OpenBSD: linux_getcwd.c,v 1.2 2001/05/16 12:50:21 ho Exp $ */
2 /* $NetBSD: vfs_getcwd.c,v 1.3.2.3 1999/07/11 10:24:09 sommerfeld Exp $ */
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 1999 The NetBSD Foundation, Inc.
7  * Copyright (c) 2015 The FreeBSD Foundation
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Bill Sommerfeld.
12  *
13  * Portions of this software were developed by Edward Tomasz Napierala
14  * under sponsorship from the FreeBSD Foundation.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include "opt_compat.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/vnode.h>
46 #include <sys/proc.h>
47 #include <sys/malloc.h>
48 
49 #ifdef COMPAT_LINUX32
50 #include <machine/../linux32/linux.h>
51 #include <machine/../linux32/linux32_proto.h>
52 #else
53 #include <machine/../linux/linux.h>
54 #include <machine/../linux/linux_proto.h>
55 #endif
56 #include <compat/linux/linux_misc.h>
57 #include <compat/linux/linux_util.h>
58 
59 /*
60  * Find pathname of process's current directory.
61  */
62 int
63 linux_getcwd(struct thread *td, struct linux_getcwd_args *uap)
64 {
65 	char *buf, *retbuf;
66 	size_t buflen;
67 	int error;
68 
69 	buflen = uap->bufsize;
70 	if (__predict_false(buflen < 2))
71 		return (ERANGE);
72 	if (buflen > LINUX_PATH_MAX)
73 		buflen = LINUX_PATH_MAX;
74 
75 	buf = malloc(buflen, M_TEMP, M_WAITOK);
76 	error = vn_getcwd(buf, &retbuf, &buflen);
77 	if (error == 0) {
78 		error = copyout(retbuf, uap->buf, buflen);
79 		if (error == 0)
80 			td->td_retval[0] = buflen;
81 	}
82 	free(buf, M_TEMP);
83 	return (error);
84 }
85