xref: /illumos-gate/usr/src/lib/libc/port/sys/fcntl.c (revision 0250c53ad267726f2438e3c6556199a0bbf588a2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  * Copyright 2024 Oxide Computer Company
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved	*/
30 
31 #include "lint.h"
32 #include <sys/param.h>
33 #include <sys/sockio.h>
34 #include <sys/filio.h>
35 #include <sys/file.h>
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #include <signal.h>
39 #include <sys/stat.h>
40 #include <sys/stropts.h>
41 #include <sys/socket.h>
42 #include <sys/stropts.h>
43 #include <sys/stream.h>
44 #include <sys/socketvar.h>
45 #include <sys/syscall.h>
46 #include <errno.h>
47 #include <stdio.h>
48 #include <stdarg.h>
49 #include <unistd.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include "libc.h"
53 
54 #if !defined(_LP64)
55 /*
56  * XXX these hacks are needed for X.25 which assumes that s_fcntl and
57  * s_ioctl exist in the socket library.
58  * There is no need for s_ioctl for other purposes.
59  */
60 #pragma weak s_fcntl = __fcntl
61 #pragma weak _s_fcntl = __fcntl
62 int
s_ioctl(int fd,int cmd,intptr_t arg)63 s_ioctl(int fd, int cmd, intptr_t arg)
64 {
65 	return (ioctl(fd, cmd, arg));
66 }
67 #endif	/* _LP64 */
68 
69 int
__fcntl(int fd,int cmd,...)70 __fcntl(int fd, int cmd, ...)
71 {
72 	int	res;
73 	int	pid;
74 	intptr_t arg, arg1 = 0;
75 	va_list ap;
76 
77 	/*
78 	 * The fcntl(2) entry points are responsible for marshalling arguments
79 	 * into intptr_t sized objects prior to calling this. The kernel only
80 	 * works in terms of intptr_t sized arguments; however, some calls (like
81 	 * F_DUP3FD) are in terms of two int sized arguments.
82 	 */
83 	va_start(ap, cmd);
84 	arg = va_arg(ap, intptr_t);
85 	if (cmd == F_DUP3FD) {
86 		arg1 = va_arg(ap, intptr_t);
87 	}
88 	va_end(ap);
89 
90 	switch (cmd) {
91 	case F_SETOWN:
92 		pid = (int)arg;
93 		return (ioctl(fd, FIOSETOWN, &pid));
94 
95 	case F_GETOWN:
96 		if (ioctl(fd, FIOGETOWN, &res) < 0)
97 			return (-1);
98 		return (res);
99 
100 	default:
101 		return (syscall(SYS_fcntl, fd, cmd, arg, arg1));
102 	}
103 }
104