1055d4956SAndrew Gallatin /* $OpenBSD: linux_getcwd.c,v 1.2 2001/05/16 12:50:21 ho Exp $ */
2055d4956SAndrew Gallatin /* $NetBSD: vfs_getcwd.c,v 1.3.2.3 1999/07/11 10:24:09 sommerfeld Exp $ */
3055d4956SAndrew Gallatin /*-
4*b61a5730SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
57f2d13d6SPedro F. Giffuni *
6055d4956SAndrew Gallatin * Copyright (c) 1999 The NetBSD Foundation, Inc.
7310e9311SEdward Tomasz Napierala * Copyright (c) 2015 The FreeBSD Foundation
8055d4956SAndrew Gallatin * All rights reserved.
9055d4956SAndrew Gallatin *
10055d4956SAndrew Gallatin * This code is derived from software contributed to The NetBSD Foundation
11055d4956SAndrew Gallatin * by Bill Sommerfeld.
12055d4956SAndrew Gallatin *
13310e9311SEdward Tomasz Napierala * Portions of this software were developed by Edward Tomasz Napierala
14310e9311SEdward Tomasz Napierala * under sponsorship from the FreeBSD Foundation.
15310e9311SEdward Tomasz Napierala *
16055d4956SAndrew Gallatin * Redistribution and use in source and binary forms, with or without
17055d4956SAndrew Gallatin * modification, are permitted provided that the following conditions
18055d4956SAndrew Gallatin * are met:
19055d4956SAndrew Gallatin * 1. Redistributions of source code must retain the above copyright
20055d4956SAndrew Gallatin * notice, this list of conditions and the following disclaimer.
21055d4956SAndrew Gallatin * 2. Redistributions in binary form must reproduce the above copyright
22055d4956SAndrew Gallatin * notice, this list of conditions and the following disclaimer in the
23055d4956SAndrew Gallatin * documentation and/or other materials provided with the distribution.
24055d4956SAndrew Gallatin *
25055d4956SAndrew Gallatin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26055d4956SAndrew Gallatin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27055d4956SAndrew Gallatin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28055d4956SAndrew Gallatin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29055d4956SAndrew Gallatin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30055d4956SAndrew Gallatin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31055d4956SAndrew Gallatin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32055d4956SAndrew Gallatin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33055d4956SAndrew Gallatin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34055d4956SAndrew Gallatin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35055d4956SAndrew Gallatin * POSSIBILITY OF SUCH DAMAGE.
36055d4956SAndrew Gallatin */
3716dbc7f2SDavid E. O'Brien
38055d4956SAndrew Gallatin #include <sys/param.h>
39cc34e37eSPoul-Henning Kamp #include <sys/malloc.h>
40d8e53d94SDmitry Chagin #include <sys/proc.h>
41d8e53d94SDmitry Chagin #include <sys/vnode.h>
42055d4956SAndrew Gallatin
431997c537SDavid E. O'Brien #ifdef COMPAT_LINUX32
444af27623STim J. Robbins #include <machine/../linux32/linux.h>
454af27623STim J. Robbins #include <machine/../linux32/linux32_proto.h>
461997c537SDavid E. O'Brien #else
471997c537SDavid E. O'Brien #include <machine/../linux/linux.h>
481997c537SDavid E. O'Brien #include <machine/../linux/linux_proto.h>
494af27623STim J. Robbins #endif
506289b482SEdward Tomasz Napierala #include <compat/linux/linux_misc.h>
51cc34e37eSPoul-Henning Kamp
52055d4956SAndrew Gallatin /*
53055d4956SAndrew Gallatin * Find pathname of process's current directory.
54055d4956SAndrew Gallatin */
55055d4956SAndrew Gallatin int
linux_getcwd(struct thread * td,struct linux_getcwd_args * uap)567739d927SMateusz Guzik linux_getcwd(struct thread *td, struct linux_getcwd_args *uap)
57055d4956SAndrew Gallatin {
587739d927SMateusz Guzik char *buf, *retbuf;
597739d927SMateusz Guzik size_t buflen;
607739d927SMateusz Guzik int error;
61cc34e37eSPoul-Henning Kamp
627739d927SMateusz Guzik buflen = uap->bufsize;
637739d927SMateusz Guzik if (__predict_false(buflen < 2))
64310e9311SEdward Tomasz Napierala return (ERANGE);
657739d927SMateusz Guzik if (buflen > LINUX_PATH_MAX)
667739d927SMateusz Guzik buflen = LINUX_PATH_MAX;
67cc34e37eSPoul-Henning Kamp
687739d927SMateusz Guzik buf = malloc(buflen, M_TEMP, M_WAITOK);
69feabaaf9SMateusz Guzik error = vn_getcwd(buf, &retbuf, &buflen);
700dfbdd9fSEdward Tomasz Napierala if (error == ENOMEM)
710dfbdd9fSEdward Tomasz Napierala error = ERANGE;
72310e9311SEdward Tomasz Napierala if (error == 0) {
737739d927SMateusz Guzik error = copyout(retbuf, uap->buf, buflen);
74310e9311SEdward Tomasz Napierala if (error == 0)
757739d927SMateusz Guzik td->td_retval[0] = buflen;
76cc34e37eSPoul-Henning Kamp }
777739d927SMateusz Guzik free(buf, M_TEMP);
78cc34e37eSPoul-Henning Kamp return (error);
79055d4956SAndrew Gallatin }
80