113546Sjulian/*
235509Sjb * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
313546Sjulian * All rights reserved.
413546Sjulian *
513546Sjulian * Redistribution and use in source and binary forms, with or without
613546Sjulian * modification, are permitted provided that the following conditions
713546Sjulian * are met:
813546Sjulian * 1. Redistributions of source code must retain the above copyright
913546Sjulian *    notice, this list of conditions and the following disclaimer.
1013546Sjulian * 2. Redistributions in binary form must reproduce the above copyright
1113546Sjulian *    notice, this list of conditions and the following disclaimer in the
1213546Sjulian *    documentation and/or other materials provided with the distribution.
13165967Simp * 3. Neither the name of the author nor the names of any co-contributors
1413546Sjulian *    may be used to endorse or promote products derived from this software
1513546Sjulian *    without specific prior written permission.
1613546Sjulian *
1713546Sjulian * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
1813546Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1913546Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2049439Sdeischen * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2113546Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2213546Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2313546Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2413546Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2513546Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2613546Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2713546Sjulian * SUCH DAMAGE.
2813546Sjulian *
2950476Speter * $FreeBSD$
3025402Sjb *
3113546Sjulian */
32174112Sdeischen
33174112Sdeischen#include "namespace.h"
3413546Sjulian#include <stdarg.h>
3517706Sjulian#include <unistd.h>
3613546Sjulian#include <fcntl.h>
3713546Sjulian#include <dirent.h>
3825402Sjb#include <errno.h>
3913546Sjulian#include <pthread.h>
40174112Sdeischen#include "un-namespace.h"
41103388Smini#include "thr_private.h"
4213546Sjulian
43174112Sdeischenint	__open(const char *path, int flags,...);
44174112Sdeischen
4575658Sdeischen__weak_reference(__open, open);
4671581Sdeischen
4713546Sjulianint
4871581Sdeischen__open(const char *path, int flags,...)
4956698Sjasone{
50113658Sdeischen	struct pthread *curthread = _get_curthread();
5156698Sjasone	int	ret;
5256698Sjasone	int	mode = 0;
5356698Sjasone	va_list	ap;
5456698Sjasone
55123312Sdavidxu	_thr_cancel_enter(curthread);
5656698Sjasone
5756698Sjasone	/* Check if the file is being created: */
5856698Sjasone	if (flags & O_CREAT) {
5956698Sjasone		/* Get the creation mode: */
6056698Sjasone		va_start(ap, flags);
6156698Sjasone		mode = va_arg(ap, int);
6256698Sjasone		va_end(ap);
6356698Sjasone	}
6456698Sjasone
65103419Smini	ret = __sys_open(path, flags, mode);
66123312Sdavidxu	/*
67123312Sdavidxu	 * To avoid possible file handle leak,
68123312Sdavidxu	 * only check cancellation point if it is failure
69123312Sdavidxu	 */
70123312Sdavidxu	_thr_cancel_leave(curthread, (ret == -1));
7156698Sjasone
7256698Sjasone	return ret;
7356698Sjasone}
74