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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1997-2000 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <sys/isa_defs.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include "libproc.h"
33 
34 /*
35  * fcntl() system call -- executed by subject process.
36  */
37 int
pr_fcntl(struct ps_prochandle * Pr,int fd,int cmd,void * argp)38 pr_fcntl(struct ps_prochandle *Pr, int fd, int cmd, void *argp)
39 {
40 	sysret_t rval;			/* return value from fcntl() */
41 	argdes_t argd[3];		/* arg descriptors for fcntl() */
42 	argdes_t *adp;
43 	int error;
44 
45 	if (Pr == NULL)		/* no subject process */
46 		return (fcntl(fd, cmd, argp));
47 
48 	adp = &argd[0];		/* file descriptor argument */
49 	adp->arg_value = fd;
50 	adp->arg_object = NULL;
51 	adp->arg_type = AT_BYVAL;
52 	adp->arg_inout = AI_INPUT;
53 	adp->arg_size = 0;
54 
55 	adp++;			/* cmd argument */
56 #ifdef _LP64
57 	if (Pstatus(Pr)->pr_dmodel == PR_MODEL_ILP32) {
58 		/*
59 		 * Guilty knowledge of the large file compilation environment
60 		 */
61 		switch (cmd) {
62 		case F_GETLK:
63 			cmd = 33;
64 			break;
65 		case F_SETLK:
66 			cmd = 34;
67 			break;
68 		case F_SETLKW:
69 			cmd = 35;
70 			break;
71 		case F_FREESP:
72 			cmd = 27;
73 			break;
74 		}
75 	}
76 #endif	/* _LP64 */
77 	adp->arg_value = cmd;
78 	adp->arg_object = NULL;
79 	adp->arg_type = AT_BYVAL;
80 	adp->arg_inout = AI_INPUT;
81 	adp->arg_size = 0;
82 
83 	adp++;			/* argp argument */
84 	if (argp == NULL) {
85 		adp->arg_value = 0;
86 		adp->arg_object = NULL;
87 		adp->arg_type = AT_BYVAL;
88 		adp->arg_inout = AI_INPUT;
89 		adp->arg_size = 0;
90 	} else {
91 		adp->arg_value = 0;
92 		adp->arg_object = argp;
93 		adp->arg_type = AT_BYREF;
94 		adp->arg_inout = AI_INOUT;
95 		switch (cmd) {
96 		case F_GETLK:
97 		case F_SETLK:
98 		case F_SETLKW:
99 		case F_ALLOCSP:
100 		case F_FREESP:
101 			adp->arg_size = sizeof (struct flock);
102 			break;
103 #ifdef _LP64
104 		case 33:
105 		case 34:
106 		case 35:
107 		case 27:
108 			adp->arg_size = sizeof (struct flock64_32);
109 #else	/* _LP64 */
110 		case F_GETLK64:
111 		case F_SETLK64:
112 		case F_SETLKW64:
113 		case F_FREESP64:
114 			adp->arg_size = sizeof (struct flock64);
115 #endif	/* _LP64 */
116 			break;
117 		case F_SHARE:
118 		case F_UNSHARE:
119 			adp->arg_size = sizeof (struct fshare);
120 			break;
121 		default:
122 			adp->arg_value = (long)argp;
123 			adp->arg_object = NULL;
124 			adp->arg_type = AT_BYVAL;
125 			adp->arg_inout = AI_INPUT;
126 			adp->arg_size = 0;
127 			break;
128 		}
129 	}
130 
131 	error = Psyscall(Pr, &rval, SYS_fcntl, 3, &argd[0]);
132 
133 	if (error) {
134 		errno = (error > 0)? error : ENOSYS;
135 		return (-1);
136 	}
137 	return (rval.sys_rval1);
138 }
139