182518Sgallatin/* $OpenBSD: linux_getcwd.c,v 1.2 2001/05/16 12:50:21 ho Exp $ */
282518Sgallatin/* $NetBSD: vfs_getcwd.c,v 1.3.2.3 1999/07/11 10:24:09 sommerfeld Exp $ */
382518Sgallatin/*-
482518Sgallatin * Copyright (c) 1999 The NetBSD Foundation, Inc.
5281882Strasz * Copyright (c) 2015 The FreeBSD Foundation
682518Sgallatin * All rights reserved.
782518Sgallatin *
882518Sgallatin * This code is derived from software contributed to The NetBSD Foundation
982518Sgallatin * by Bill Sommerfeld.
1082518Sgallatin *
11281882Strasz * Portions of this software were developed by Edward Tomasz Napierala
12281882Strasz * under sponsorship from the FreeBSD Foundation.
13281882Strasz *
1482518Sgallatin * Redistribution and use in source and binary forms, with or without
1582518Sgallatin * modification, are permitted provided that the following conditions
1682518Sgallatin * are met:
1782518Sgallatin * 1. Redistributions of source code must retain the above copyright
1882518Sgallatin *    notice, this list of conditions and the following disclaimer.
1982518Sgallatin * 2. Redistributions in binary form must reproduce the above copyright
2082518Sgallatin *    notice, this list of conditions and the following disclaimer in the
2182518Sgallatin *    documentation and/or other materials provided with the distribution.
2282518Sgallatin *
2382518Sgallatin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2482518Sgallatin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2582518Sgallatin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2682518Sgallatin * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2782518Sgallatin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2882518Sgallatin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2982518Sgallatin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3082518Sgallatin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3182518Sgallatin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3282518Sgallatin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3382518Sgallatin * POSSIBILITY OF SUCH DAMAGE.
3482518Sgallatin */
35116173Sobrien
36116173Sobrien#include <sys/cdefs.h>
37116173Sobrien__FBSDID("$FreeBSD$");
38116173Sobrien
39156874Sru#include "opt_compat.h"
4082518Sgallatin
4182518Sgallatin#include <sys/param.h>
4282518Sgallatin#include <sys/systm.h>
43102872Siedowse#include <sys/syscallsubr.h>
4482518Sgallatin#include <sys/proc.h>
45112430Sphk#include <sys/malloc.h>
4682518Sgallatin
47140214Sobrien#ifdef COMPAT_LINUX32
48140214Sobrien#include <machine/../linux32/linux.h>
49140214Sobrien#include <machine/../linux32/linux32_proto.h>
50140214Sobrien#else
5182518Sgallatin#include <machine/../linux/linux.h>
5282518Sgallatin#include <machine/../linux/linux_proto.h>
53133816Stjr#endif
54281829Strasz#include <compat/linux/linux_misc.h>
55112430Sphk#include <compat/linux/linux_util.h>
5682518Sgallatin
5782518Sgallatin/*
5882518Sgallatin * Find pathname of process's current directory.
5982518Sgallatin */
6082518Sgallatinint
6183366Sjulianlinux_getcwd(struct thread *td, struct linux_getcwd_args *args)
6282518Sgallatin{
63281882Strasz	char *path;
64281882Strasz	int error, lenused;
65112430Sphk
66112430Sphk#ifdef DEBUG
67164379Skib	if (ldebug(getcwd))
68164383Skib		printf(ARGS(getcwd, "%p, %ld"), args->buf, (long)args->bufsize);
69112430Sphk#endif
70112430Sphk
71281882Strasz	/*
72281882Strasz	 * Linux returns ERANGE instead of EINVAL.
73281882Strasz	 */
74281882Strasz	if (args->bufsize < 2)
75281882Strasz		return (ERANGE);
76112430Sphk
77283427Sdchagin	path = malloc(LINUX_PATH_MAX, M_LINUX, M_WAITOK);
78112430Sphk
79281882Strasz	error = kern___getcwd(td, path, UIO_SYSSPACE, args->bufsize,
80281882Strasz	    LINUX_PATH_MAX);
81281882Strasz	if (error == 0) {
82112430Sphk		lenused = strlen(path) + 1;
83281882Strasz		error = copyout(path, args->buf, lenused);
84281882Strasz		if (error == 0)
85112430Sphk			td->td_retval[0] = lenused;
86281882Strasz	}
87112430Sphk
88283427Sdchagin	free(path, M_LINUX);
89112430Sphk	return (error);
9082518Sgallatin}
91