file.c (8ca842c4b5cbc70b9180617e9f26b6ac9f40dbb9) file.c (512b6fb1c14d4c34f23a3419b0789ad01914a899)
1/*
2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <stdio.h>
7#include <unistd.h>
8#include <errno.h>

--- 68 unchanged lines hidden (view full) ---

77
78 err = access(file, amode);
79 if(err < 0)
80 return -errno;
81
82 return 0;
83}
84
1/*
2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <stdio.h>
7#include <unistd.h>
8#include <errno.h>

--- 68 unchanged lines hidden (view full) ---

77
78 err = access(file, amode);
79 if(err < 0)
80 return -errno;
81
82 return 0;
83}
84
85void os_print_error(int error, const char* str)
86{
87 errno = error < 0 ? -error : error;
88
89 perror(str);
90}
91
92/* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
93int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
94{
95 int err;
96
97 err = ioctl(fd, cmd, arg);
98 if(err < 0)
99 return -errno;

--- 76 unchanged lines hidden (view full) ---

176}
177
178int os_file_mode(char *file, struct openflags *mode_out)
179{
180 int err;
181
182 *mode_out = OPENFLAGS();
183
85/* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
86int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
87{
88 int err;
89
90 err = ioctl(fd, cmd, arg);
91 if(err < 0)
92 return -errno;

--- 76 unchanged lines hidden (view full) ---

169}
170
171int os_file_mode(char *file, struct openflags *mode_out)
172{
173 int err;
174
175 *mode_out = OPENFLAGS();
176
184 err = os_access(file, OS_ACC_W_OK);
185 if((err < 0) && (err != -EACCES))
186 return(err);
177 err = access(file, W_OK);
178 if(err && (errno != EACCES))
179 return -errno;
180 else if(!err)
181 *mode_out = of_write(*mode_out);
187
182
188 *mode_out = of_write(*mode_out);
183 err = access(file, R_OK);
184 if(err && (errno != EACCES))
185 return -errno;
186 else if(!err)
187 *mode_out = of_read(*mode_out);
189
188
190 err = os_access(file, OS_ACC_R_OK);
191 if((err < 0) && (err != -EACCES))
192 return(err);
193
194 *mode_out = of_read(*mode_out);
195
196 return(0);
189 return err;
197}
198
199int os_open_file(char *file, struct openflags flags, int mode)
200{
201 int fd, err, f = 0;
202
203 if(flags.r && flags.w) f = O_RDWR;
204 else if(flags.r) f = O_RDONLY;
205 else if(flags.w) f = O_WRONLY;
206 else f = 0;
207
208 if(flags.s) f |= O_SYNC;
209 if(flags.c) f |= O_CREAT;
210 if(flags.t) f |= O_TRUNC;
211 if(flags.e) f |= O_EXCL;
212
213 fd = open64(file, f, mode);
214 if(fd < 0)
190}
191
192int os_open_file(char *file, struct openflags flags, int mode)
193{
194 int fd, err, f = 0;
195
196 if(flags.r && flags.w) f = O_RDWR;
197 else if(flags.r) f = O_RDONLY;
198 else if(flags.w) f = O_WRONLY;
199 else f = 0;
200
201 if(flags.s) f |= O_SYNC;
202 if(flags.c) f |= O_CREAT;
203 if(flags.t) f |= O_TRUNC;
204 if(flags.e) f |= O_EXCL;
205
206 fd = open64(file, f, mode);
207 if(fd < 0)
215 return(-errno);
208 return -errno;
216
217 if(flags.cl && fcntl(fd, F_SETFD, 1)){
218 err = -errno;
209
210 if(flags.cl && fcntl(fd, F_SETFD, 1)){
211 err = -errno;
219 os_close_file(fd);
212 close(fd);
220 return err;
221 }
222
213 return err;
214 }
215
223 return(fd);
216 return fd;
224}
225
226int os_connect_socket(char *name)
227{
228 struct sockaddr_un sock;
229 int fd, err;
230
231 sock.sun_family = AF_UNIX;

--- 55 unchanged lines hidden (view full) ---

287int os_file_size(char *file, unsigned long long *size_out)
288{
289 struct uml_stat buf;
290 int err;
291
292 err = os_stat_file(file, &buf);
293 if(err < 0){
294 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
217}
218
219int os_connect_socket(char *name)
220{
221 struct sockaddr_un sock;
222 int fd, err;
223
224 sock.sun_family = AF_UNIX;

--- 55 unchanged lines hidden (view full) ---

280int os_file_size(char *file, unsigned long long *size_out)
281{
282 struct uml_stat buf;
283 int err;
284
285 err = os_stat_file(file, &buf);
286 if(err < 0){
287 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
295 return(err);
288 return err;
296 }
297
298 if(S_ISBLK(buf.ust_mode)){
299 int fd;
300 long blocks;
301
289 }
290
291 if(S_ISBLK(buf.ust_mode)){
292 int fd;
293 long blocks;
294
302 fd = os_open_file(file, of_read(OPENFLAGS()), 0);
303 if(fd < 0){
304 printk("Couldn't open \"%s\", errno = %d\n", file, -fd);
305 return(fd);
295 fd = open(file, O_RDONLY, 0);
296 if(fd < 0) {
297 err = -errno;
298 printk("Couldn't open \"%s\", errno = %d\n", file,
299 errno);
300 return err;
306 }
307 if(ioctl(fd, BLKGETSIZE, &blocks) < 0){
308 err = -errno;
309 printk("Couldn't get the block size of \"%s\", "
310 "errno = %d\n", file, errno);
301 }
302 if(ioctl(fd, BLKGETSIZE, &blocks) < 0){
303 err = -errno;
304 printk("Couldn't get the block size of \"%s\", "
305 "errno = %d\n", file, errno);
311 os_close_file(fd);
312 return(err);
306 close(fd);
307 return err;
313 }
314 *size_out = ((long long) blocks) * 512;
308 }
309 *size_out = ((long long) blocks) * 512;
315 os_close_file(fd);
316 return(0);
310 close(fd);
317 }
311 }
318 *size_out = buf.ust_size;
319 return(0);
312 else *size_out = buf.ust_size;
313
314 return 0;
320}
321
322int os_file_modtime(char *file, unsigned long *modtime)
323{
324 struct uml_stat buf;
325 int err;
326
327 err = os_stat_file(file, &buf);
328 if(err < 0){
329 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
330 return err;
331 }
332
333 *modtime = buf.ust_mtime;
334 return 0;
335}
336
315}
316
317int os_file_modtime(char *file, unsigned long *modtime)
318{
319 struct uml_stat buf;
320 int err;
321
322 err = os_stat_file(file, &buf);
323 if(err < 0){
324 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
325 return err;
326 }
327
328 *modtime = buf.ust_mtime;
329 return 0;
330}
331
337int os_get_exec_close(int fd, int* close_on_exec)
332int os_get_exec_close(int fd, int *close_on_exec)
338{
339 int ret;
340
333{
334 int ret;
335
341 do {
342 ret = fcntl(fd, F_GETFD);
343 } while((ret < 0) && (errno == EINTR)) ;
336 CATCH_EINTR(ret = fcntl(fd, F_GETFD));
344
345 if(ret < 0)
337
338 if(ret < 0)
346 return(-errno);
339 return -errno;
347
340
348 *close_on_exec = (ret&FD_CLOEXEC) ? 1 : 0;
349 return(ret);
341 *close_on_exec = (ret & FD_CLOEXEC) ? 1 : 0;
342 return ret;
350}
351
343}
344
352int os_set_exec_close(int fd, int close_on_exec)
345int os_set_exec_close(int fd)
353{
346{
354 int flag, err;
347 int err;
355
348
356 if(close_on_exec) flag = FD_CLOEXEC;
357 else flag = 0;
349 CATCH_EINTR(err = fcntl(fd, F_SETFD, FD_CLOEXEC));
358
350
359 do {
360 err = fcntl(fd, F_SETFD, flag);
361 } while((err < 0) && (errno == EINTR)) ;
362
363 if(err < 0)
351 if(err < 0)
364 return(-errno);
365 return(err);
352 return -errno;
353 return err;
366}
367
368int os_pipe(int *fds, int stream, int close_on_exec)
369{
370 int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
371
372 err = socketpair(AF_UNIX, type, 0, fds);
373 if(err < 0)
354}
355
356int os_pipe(int *fds, int stream, int close_on_exec)
357{
358 int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
359
360 err = socketpair(AF_UNIX, type, 0, fds);
361 if(err < 0)
374 return(-errno);
362 return -errno;
375
376 if(!close_on_exec)
363
364 if(!close_on_exec)
377 return(0);
365 return 0;
378
366
379 err = os_set_exec_close(fds[0], 1);
367 err = os_set_exec_close(fds[0]);
380 if(err < 0)
381 goto error;
382
368 if(err < 0)
369 goto error;
370
383 err = os_set_exec_close(fds[1], 1);
371 err = os_set_exec_close(fds[1]);
384 if(err < 0)
385 goto error;
386
387 return 0;
388
389 error:
390 printk("os_pipe : Setting FD_CLOEXEC failed, err = %d\n", -err);
372 if(err < 0)
373 goto error;
374
375 return 0;
376
377 error:
378 printk("os_pipe : Setting FD_CLOEXEC failed, err = %d\n", -err);
391 os_close_file(fds[1]);
392 os_close_file(fds[0]);
393 return(err);
379 close(fds[1]);
380 close(fds[0]);
381 return err;
394}
395
396int os_set_fd_async(int fd, int owner)
397{
398 int err;
399
400 /* XXX This should do F_GETFL first */
401 if(fcntl(fd, F_SETFL, O_ASYNC | O_NONBLOCK) < 0){

--- 130 unchanged lines hidden (view full) ---

532 struct sockaddr_un addr;
533 int sock, err;
534
535 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
536 if(sock < 0)
537 return -errno;
538
539 if(close_on_exec) {
382}
383
384int os_set_fd_async(int fd, int owner)
385{
386 int err;
387
388 /* XXX This should do F_GETFL first */
389 if(fcntl(fd, F_SETFL, O_ASYNC | O_NONBLOCK) < 0){

--- 130 unchanged lines hidden (view full) ---

520 struct sockaddr_un addr;
521 int sock, err;
522
523 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
524 if(sock < 0)
525 return -errno;
526
527 if(close_on_exec) {
540 err = os_set_exec_close(sock, 1);
528 err = os_set_exec_close(sock);
541 if(err < 0)
542 printk("create_unix_socket : close_on_exec failed, "
543 "err = %d", -err);
544 }
545
546 addr.sun_family = AF_UNIX;
547
548 /* XXX Be more careful about overflow */

--- 39 unchanged lines hidden ---
529 if(err < 0)
530 printk("create_unix_socket : close_on_exec failed, "
531 "err = %d", -err);
532 }
533
534 addr.sun_family = AF_UNIX;
535
536 /* XXX Be more careful about overflow */

--- 39 unchanged lines hidden ---