vm: restore stacktrace on SIGSEGV
[minix.git] / servers / vfs / utility.c
blobb3f078c53e8f42f2041884326a07840aa762aad3
1 /* This file contains a few general purpose utility routines.
3 * The entry points into this file are
4 * clock_time: ask the clock task for the real time
5 * copy: copy a block of data
6 * fetch_name: go get a path name from user space
7 * no_sys: reject a system call that FS does not handle
8 * panic: something awful has occurred; MINIX cannot continue
9 * conv2: do byte swapping on a 16-bit int
10 * conv4: do byte swapping on a 32-bit long
11 * in_group: determines if group 'grp' is in rfp->fp_sgroups[]
14 #include "fs.h"
15 #include <minix/com.h>
16 #include <minix/endpoint.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <assert.h>
21 #include "file.h"
22 #include "fproc.h"
23 #include "param.h"
24 #include "vmnt.h"
26 /*===========================================================================*
27 * copy_name *
28 *===========================================================================*/
29 inline int copy_name( size_t len, char *dest)
31 /* Go get path and put it in 'dest'.
33 if (len > PATH_MAX) { /* 'len' includes terminating-nul */
34 err_code = ENAMETOOLONG;
35 return(EGENERIC);
38 /* Check name length for validity. */
39 if (len > SSIZE_MAX) {
40 err_code = EINVAL;
41 return(EGENERIC);
44 if (len <= M3_STRING) {
45 /* Just copy the path from the message */
46 strncpy(dest, job_m_in.pathname, len);
47 } else {
48 /* String is not contained in the message. */
49 err_code = EINVAL;
50 return(EGENERIC);
53 if (dest[len - 1] != '\0') {
54 err_code = ENAMETOOLONG;
55 return(EGENERIC);
58 return(OK);
61 /*===========================================================================*
62 * fetch_name *
63 *===========================================================================*/
64 int fetch_name(vir_bytes path, size_t len, char *dest)
66 /* Go get path and put it in 'dest'. */
67 int r;
69 if (len > PATH_MAX) { /* 'len' includes terminating-nul */
70 err_code = ENAMETOOLONG;
71 return(EGENERIC);
74 /* Check name length for validity. */
75 if (len > SSIZE_MAX) {
76 err_code = EINVAL;
77 return(EGENERIC);
80 /* String is not contained in the message. Get it from user space. */
81 r = sys_datacopy(who_e, path, VFS_PROC_NR, (vir_bytes) dest, len);
82 if (r != OK) {
83 err_code = EINVAL;
84 return(r);
87 if (dest[len - 1] != '\0') {
88 err_code = ENAMETOOLONG;
89 return(EGENERIC);
92 return(OK);
96 /*===========================================================================*
97 * no_sys *
98 *===========================================================================*/
99 int no_sys()
101 /* Somebody has used an illegal system call number */
102 return(ENOSYS);
106 /*===========================================================================*
107 * isokendpt_f *
108 *===========================================================================*/
109 int isokendpt_f(char *file, int line, endpoint_t endpoint, int *proc,
110 int fatal)
112 int failed = 0;
113 endpoint_t ke;
114 *proc = _ENDPOINT_P(endpoint);
115 if (endpoint == NONE) {
116 printf("VFS %s:%d: endpoint is NONE\n", file, line);
117 failed = 1;
118 } else if (*proc < 0 || *proc >= NR_PROCS) {
119 printf("VFS %s:%d: proc (%d) from endpoint (%d) out of range\n",
120 file, line, *proc, endpoint);
121 failed = 1;
122 } else if ((ke = fproc[*proc].fp_endpoint) != endpoint) {
123 if(ke == NONE) {
124 printf("VFS %s:%d: endpoint (%d) points to NONE slot (%d)\n",
125 file, line, endpoint, *proc);
126 assert(fproc[*proc].fp_pid == PID_FREE);
127 } else {
128 printf("VFS %s:%d: proc (%d) from endpoint (%d) doesn't match "
129 "known endpoint (%d)\n", file, line, *proc, endpoint,
130 fproc[*proc].fp_endpoint);
131 assert(fproc[*proc].fp_pid != PID_FREE);
133 failed = 1;
136 if(failed && fatal)
137 panic("isokendpt_f failed");
139 return(failed ? EDEADEPT : OK);
143 /*===========================================================================*
144 * clock_time *
145 *===========================================================================*/
146 time_t clock_time()
148 /* This routine returns the time in seconds since 1.1.1970. MINIX is an
149 * astrophysically naive system that assumes the earth rotates at a constant
150 * rate and that such things as leap seconds do not exist.
153 register int r;
154 clock_t uptime;
155 time_t boottime;
157 r = getuptime2(&uptime, &boottime);
158 if (r != OK)
159 panic("clock_time err: %d", r);
161 return( (time_t) (boottime + (uptime/system_hz)));
164 /*===========================================================================*
165 * in_group *
166 *===========================================================================*/
167 int in_group(struct fproc *rfp, gid_t grp)
169 int i;
171 for (i = 0; i < rfp->fp_ngroups; i++)
172 if (rfp->fp_sgroups[i] == grp)
173 return(OK);
175 return(EINVAL);