Commit 1adfd6095e1c654dce5a692db5aa5a2b2a8d6b0d

Authored by Jeff Dike
Committed by Linus Torvalds
1 parent bf8fde785b

uml: style fixes in file.c

arch/um/os-Linux/file.c needed some style work -
	updated the copyright
	cleaned up the includes
	CodingStyle fixes
	added some missing CATCH_EINTRs
	os_set_owner was unused, so it is gone
	all printks now have severities
	fcntl(F_GETFL) was being called without checking the return
	removed an obsolete comment

Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 2 changed files with 118 additions and 115 deletions Side-by-side Diff

arch/um/include/os.h
... ... @@ -131,7 +131,6 @@
131 131 extern int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg);
132 132 extern int os_get_ifname(int fd, char *namebuf);
133 133 extern int os_set_slip(int fd);
134   -extern int os_set_owner(int fd, int pid);
135 134 extern int os_mode_fd(int fd, int mode);
136 135  
137 136 extern int os_seek_file(int fd, unsigned long long offset);
arch/um/os-Linux/file.c
1 1 /*
2   - * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
  2 + * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 3 * Licensed under the GPL
4 4 */
5 5  
6 6  
... ... @@ -8,13 +8,12 @@
8 8 #include <errno.h>
9 9 #include <fcntl.h>
10 10 #include <signal.h>
11   -#include <sys/types.h>
12   -#include <sys/stat.h>
13   -#include <sys/socket.h>
14   -#include <sys/un.h>
15 11 #include <sys/ioctl.h>
16 12 #include <sys/mount.h>
17   -#include <sys/uio.h>
  13 +#include <sys/socket.h>
  14 +#include <sys/stat.h>
  15 +#include <sys/un.h>
  16 +#include "kern_constants.h"
18 17 #include "os.h"
19 18 #include "user.h"
20 19  
21 20  
... ... @@ -42,10 +41,10 @@
42 41 int err;
43 42  
44 43 CATCH_EINTR(err = fstat64(fd, &sbuf));
45   - if(err < 0)
  44 + if (err < 0)
46 45 return -errno;
47 46  
48   - if(ubuf != NULL)
  47 + if (ubuf != NULL)
49 48 copy_stat(ubuf, &sbuf);
50 49 return err;
51 50 }
52 51  
53 52  
54 53  
55 54  
... ... @@ -55,27 +54,26 @@
55 54 struct stat64 sbuf;
56 55 int err;
57 56  
58   - do {
59   - err = stat64(file_name, &sbuf);
60   - } while((err < 0) && (errno == EINTR)) ;
61   -
62   - if(err < 0)
  57 + CATCH_EINTR(err = stat64(file_name, &sbuf));
  58 + if (err < 0)
63 59 return -errno;
64 60  
65   - if(ubuf != NULL)
  61 + if (ubuf != NULL)
66 62 copy_stat(ubuf, &sbuf);
67 63 return err;
68 64 }
69 65  
70   -int os_access(const char* file, int mode)
  66 +int os_access(const char *file, int mode)
71 67 {
72 68 int amode, err;
73 69  
74   - amode=(mode&OS_ACC_R_OK ? R_OK : 0) | (mode&OS_ACC_W_OK ? W_OK : 0) |
75   - (mode&OS_ACC_X_OK ? X_OK : 0) | (mode&OS_ACC_F_OK ? F_OK : 0) ;
  70 + amode = (mode & OS_ACC_R_OK ? R_OK : 0) |
  71 + (mode & OS_ACC_W_OK ? W_OK : 0) |
  72 + (mode & OS_ACC_X_OK ? X_OK : 0) |
  73 + (mode & OS_ACC_F_OK ? F_OK : 0);
76 74  
77 75 err = access(file, amode);
78   - if(err < 0)
  76 + if (err < 0)
79 77 return -errno;
80 78  
81 79 return 0;
... ... @@ -87,7 +85,7 @@
87 85 int err;
88 86  
89 87 err = ioctl(fd, cmd, arg);
90   - if(err < 0)
  88 + if (err < 0)
91 89 return -errno;
92 90  
93 91 return err;
... ... @@ -96,7 +94,7 @@
96 94 /* FIXME: ensure namebuf in os_get_if_name is big enough */
97 95 int os_get_ifname(int fd, char* namebuf)
98 96 {
99   - if(ioctl(fd, SIOCGIFNAME, namebuf) < 0)
  97 + if (ioctl(fd, SIOCGIFNAME, namebuf) < 0)
100 98 return -errno;
101 99  
102 100 return 0;
103 101  
104 102  
105 103  
... ... @@ -107,37 +105,22 @@
107 105 int disc, sencap;
108 106  
109 107 disc = N_SLIP;
110   - if(ioctl(fd, TIOCSETD, &disc) < 0)
  108 + if (ioctl(fd, TIOCSETD, &disc) < 0)
111 109 return -errno;
112 110  
113 111 sencap = 0;
114   - if(ioctl(fd, SIOCSIFENCAP, &sencap) < 0)
  112 + if (ioctl(fd, SIOCSIFENCAP, &sencap) < 0)
115 113 return -errno;
116 114  
117 115 return 0;
118 116 }
119 117  
120   -int os_set_owner(int fd, int pid)
121   -{
122   - if(fcntl(fd, F_SETOWN, pid) < 0){
123   - int save_errno = errno;
124   -
125   - if(fcntl(fd, F_GETOWN, 0) != pid)
126   - return -save_errno;
127   - }
128   -
129   - return 0;
130   -}
131   -
132 118 int os_mode_fd(int fd, int mode)
133 119 {
134 120 int err;
135 121  
136   - do {
137   - err = fchmod(fd, mode);
138   - } while((err < 0) && (errno==EINTR)) ;
139   -
140   - if(err < 0)
  122 + CATCH_EINTR(err = fchmod(fd, mode));
  123 + if (err < 0)
141 124 return -errno;
142 125  
143 126 return 0;
144 127  
145 128  
146 129  
147 130  
148 131  
149 132  
... ... @@ -149,20 +132,20 @@
149 132 int err;
150 133  
151 134 err = os_stat_file(file, &buf);
152   - if(err < 0)
  135 + if (err < 0)
153 136 return err;
154 137  
155   - if(S_ISDIR(buf.ust_mode))
  138 + if (S_ISDIR(buf.ust_mode))
156 139 return OS_TYPE_DIR;
157   - else if(S_ISLNK(buf.ust_mode))
  140 + else if (S_ISLNK(buf.ust_mode))
158 141 return OS_TYPE_SYMLINK;
159   - else if(S_ISCHR(buf.ust_mode))
  142 + else if (S_ISCHR(buf.ust_mode))
160 143 return OS_TYPE_CHARDEV;
161   - else if(S_ISBLK(buf.ust_mode))
  144 + else if (S_ISBLK(buf.ust_mode))
162 145 return OS_TYPE_BLOCKDEV;
163   - else if(S_ISFIFO(buf.ust_mode))
  146 + else if (S_ISFIFO(buf.ust_mode))
164 147 return OS_TYPE_FIFO;
165   - else if(S_ISSOCK(buf.ust_mode))
  148 + else if (S_ISSOCK(buf.ust_mode))
166 149 return OS_TYPE_SOCK;
167 150 else return OS_TYPE_FILE;
168 151 }
169 152  
170 153  
171 154  
... ... @@ -174,15 +157,15 @@
174 157 *mode_out = OPENFLAGS();
175 158  
176 159 err = access(file, W_OK);
177   - if(err && (errno != EACCES))
  160 + if (err && (errno != EACCES))
178 161 return -errno;
179   - else if(!err)
  162 + else if (!err)
180 163 *mode_out = of_write(*mode_out);
181 164  
182 165 err = access(file, R_OK);
183   - if(err && (errno != EACCES))
  166 + if (err && (errno != EACCES))
184 167 return -errno;
185   - else if(!err)
  168 + else if (!err)
186 169 *mode_out = of_read(*mode_out);
187 170  
188 171 return err;
189 172  
190 173  
191 174  
... ... @@ -192,21 +175,28 @@
192 175 {
193 176 int fd, err, f = 0;
194 177  
195   - if(flags.r && flags.w) f = O_RDWR;
196   - else if(flags.r) f = O_RDONLY;
197   - else if(flags.w) f = O_WRONLY;
  178 + if (flags.r && flags.w)
  179 + f = O_RDWR;
  180 + else if (flags.r)
  181 + f = O_RDONLY;
  182 + else if (flags.w)
  183 + f = O_WRONLY;
198 184 else f = 0;
199 185  
200   - if(flags.s) f |= O_SYNC;
201   - if(flags.c) f |= O_CREAT;
202   - if(flags.t) f |= O_TRUNC;
203   - if(flags.e) f |= O_EXCL;
  186 + if (flags.s)
  187 + f |= O_SYNC;
  188 + if (flags.c)
  189 + f |= O_CREAT;
  190 + if (flags.t)
  191 + f |= O_TRUNC;
  192 + if (flags.e)
  193 + f |= O_EXCL;
204 194  
205 195 fd = open64(file, f, mode);
206   - if(fd < 0)
  196 + if (fd < 0)
207 197 return -errno;
208 198  
209   - if(flags.cl && fcntl(fd, F_SETFD, 1)){
  199 + if (flags.cl && fcntl(fd, F_SETFD, 1)) {
210 200 err = -errno;
211 201 close(fd);
212 202 return err;
213 203  
... ... @@ -224,13 +214,13 @@
224 214 snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
225 215  
226 216 fd = socket(AF_UNIX, SOCK_STREAM, 0);
227   - if(fd < 0) {
  217 + if (fd < 0) {
228 218 err = -errno;
229 219 goto out;
230 220 }
231 221  
232 222 err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
233   - if(err) {
  223 + if (err) {
234 224 err = -errno;
235 225 goto out_close;
236 226 }
... ... @@ -253,7 +243,7 @@
253 243 unsigned long long actual;
254 244  
255 245 actual = lseek64(fd, offset, SEEK_SET);
256   - if(actual != offset)
  246 + if (actual != offset)
257 247 return -errno;
258 248 return 0;
259 249 }
... ... @@ -262,7 +252,7 @@
262 252 {
263 253 int n = read(fd, buf, len);
264 254  
265   - if(n < 0)
  255 + if (n < 0)
266 256 return -errno;
267 257 return n;
268 258 }
... ... @@ -271,7 +261,7 @@
271 261 {
272 262 int n = write(fd, (void *) buf, len);
273 263  
274   - if(n < 0)
  264 + if (n < 0)
275 265 return -errno;
276 266 return n;
277 267 }
278 268  
279 269  
280 270  
281 271  
282 272  
... ... @@ -282,26 +272,27 @@
282 272 int err;
283 273  
284 274 err = os_stat_file(file, &buf);
285   - if(err < 0){
286   - printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
  275 + if (err < 0) {
  276 + printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
  277 + -err);
287 278 return err;
288 279 }
289 280  
290   - if(S_ISBLK(buf.ust_mode)){
  281 + if (S_ISBLK(buf.ust_mode)) {
291 282 int fd;
292 283 long blocks;
293 284  
294 285 fd = open(file, O_RDONLY, 0);
295   - if(fd < 0) {
  286 + if (fd < 0) {
296 287 err = -errno;
297   - printk("Couldn't open \"%s\", errno = %d\n", file,
298   - errno);
  288 + printk(UM_KERN_ERR "Couldn't open \"%s\", "
  289 + "errno = %d\n", file, errno);
299 290 return err;
300 291 }
301   - if(ioctl(fd, BLKGETSIZE, &blocks) < 0){
  292 + if (ioctl(fd, BLKGETSIZE, &blocks) < 0) {
302 293 err = -errno;
303   - printk("Couldn't get the block size of \"%s\", "
304   - "errno = %d\n", file, errno);
  294 + printk(UM_KERN_ERR "Couldn't get the block size of "
  295 + "\"%s\", errno = %d\n", file, errno);
305 296 close(fd);
306 297 return err;
307 298 }
... ... @@ -319,8 +310,9 @@
319 310 int err;
320 311  
321 312 err = os_stat_file(file, &buf);
322   - if(err < 0){
323   - printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
  313 + if (err < 0) {
  314 + printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
  315 + -err);
324 316 return err;
325 317 }
326 318  
... ... @@ -334,7 +326,7 @@
334 326  
335 327 CATCH_EINTR(err = fcntl(fd, F_SETFD, FD_CLOEXEC));
336 328  
337   - if(err < 0)
  329 + if (err < 0)
338 330 return -errno;
339 331 return err;
340 332 }
341 333  
342 334  
343 335  
344 336  
... ... @@ -344,24 +336,25 @@
344 336 int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
345 337  
346 338 err = socketpair(AF_UNIX, type, 0, fds);
347   - if(err < 0)
  339 + if (err < 0)
348 340 return -errno;
349 341  
350   - if(!close_on_exec)
  342 + if (!close_on_exec)
351 343 return 0;
352 344  
353 345 err = os_set_exec_close(fds[0]);
354   - if(err < 0)
  346 + if (err < 0)
355 347 goto error;
356 348  
357 349 err = os_set_exec_close(fds[1]);
358   - if(err < 0)
  350 + if (err < 0)
359 351 goto error;
360 352  
361 353 return 0;
362 354  
363 355 error:
364   - printk("os_pipe : Setting FD_CLOEXEC failed, err = %d\n", -err);
  356 + printk(UM_KERN_ERR "os_pipe : Setting FD_CLOEXEC failed, err = %d\n",
  357 + -err);
365 358 close(fds[1]);
366 359 close(fds[0]);
367 360 return err;
368 361  
... ... @@ -378,15 +371,15 @@
378 371 flags |= O_ASYNC | O_NONBLOCK;
379 372 if (fcntl(fd, F_SETFL, flags) < 0) {
380 373 err = -errno;
381   - printk("os_set_fd_async : failed to set O_ASYNC and "
382   - "O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
  374 + printk(UM_KERN_ERR "os_set_fd_async : failed to set O_ASYNC "
  375 + "and O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
383 376 return err;
384 377 }
385 378  
386 379 if ((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
387 380 (fcntl(fd, F_SETOWN, os_getpid()) < 0)) {
388 381 err = -errno;
389   - printk("os_set_fd_async : Failed to fcntl F_SETOWN "
  382 + printk(UM_KERN_ERR "os_set_fd_async : Failed to fcntl F_SETOWN "
390 383 "(or F_SETSIG) fd %d, errno = %d\n", fd, errno);
391 384 return err;
392 385 }
393 386  
394 387  
... ... @@ -396,10 +389,14 @@
396 389  
397 390 int os_clear_fd_async(int fd)
398 391 {
399   - int flags = fcntl(fd, F_GETFL);
  392 + int flags;
400 393  
  394 + flags = fcntl(fd, F_GETFL);
  395 + if (flags < 0)
  396 + return -errno;
  397 +
401 398 flags &= ~(O_ASYNC | O_NONBLOCK);
402   - if(fcntl(fd, F_SETFL, flags) < 0)
  399 + if (fcntl(fd, F_SETFL, flags) < 0)
403 400 return -errno;
404 401 return 0;
405 402 }
406 403  
407 404  
... ... @@ -409,11 +406,15 @@
409 406 int flags;
410 407  
411 408 flags = fcntl(fd, F_GETFL);
  409 + if (flags < 0)
  410 + return -errno;
412 411  
413   - if(blocking) flags &= ~O_NONBLOCK;
414   - else flags |= O_NONBLOCK;
  412 + if (blocking)
  413 + flags &= ~O_NONBLOCK;
  414 + else
  415 + flags |= O_NONBLOCK;
415 416  
416   - if(fcntl(fd, F_SETFL, flags) < 0)
  417 + if (fcntl(fd, F_SETFL, flags) < 0)
417 418 return -errno;
418 419  
419 420 return 0;
... ... @@ -424,7 +425,7 @@
424 425 int new;
425 426  
426 427 new = accept(fd, NULL, 0);
427   - if(new < 0)
  428 + if (new < 0)
428 429 return -errno;
429 430 return new;
430 431 }
431 432  
432 433  
... ... @@ -445,15 +446,17 @@
445 446 {
446 447 int what, err;
447 448  
448   - if(r && w) what = SHUT_RDWR;
449   - else if(r) what = SHUT_RD;
450   - else if(w) what = SHUT_WR;
451   - else {
452   - printk("os_shutdown_socket : neither r or w was set\n");
  449 + if (r && w)
  450 + what = SHUT_RDWR;
  451 + else if (r)
  452 + what = SHUT_RD;
  453 + else if (w)
  454 + what = SHUT_WR;
  455 + else
453 456 return -EINVAL;
454   - }
  457 +
455 458 err = shutdown(fd, what);
456   - if(err < 0)
  459 + if (err < 0)
457 460 return -errno;
458 461 return 0;
459 462 }
460 463  
461 464  
462 465  
... ... @@ -477,19 +480,20 @@
477 480 msg.msg_flags = 0;
478 481  
479 482 n = recvmsg(fd, &msg, 0);
480   - if(n < 0)
  483 + if (n < 0)
481 484 return -errno;
482   - else if(n != iov.iov_len)
  485 + else if (n != iov.iov_len)
483 486 *helper_pid_out = -1;
484 487  
485 488 cmsg = CMSG_FIRSTHDR(&msg);
486   - if(cmsg == NULL){
487   - printk("rcv_fd didn't receive anything, error = %d\n", errno);
  489 + if (cmsg == NULL) {
  490 + printk(UM_KERN_ERR "rcv_fd didn't receive anything, "
  491 + "error = %d\n", errno);
488 492 return -1;
489 493 }
490   - if((cmsg->cmsg_level != SOL_SOCKET) ||
491   - (cmsg->cmsg_type != SCM_RIGHTS)){
492   - printk("rcv_fd didn't receive a descriptor\n");
  494 + if ((cmsg->cmsg_level != SOL_SOCKET) ||
  495 + (cmsg->cmsg_type != SCM_RIGHTS)) {
  496 + printk(UM_KERN_ERR "rcv_fd didn't receive a descriptor\n");
493 497 return -1;
494 498 }
495 499  
496 500  
497 501  
498 502  
499 503  
... ... @@ -503,23 +507,22 @@
503 507 int sock, err;
504 508  
505 509 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
506   - if(sock < 0)
  510 + if (sock < 0)
507 511 return -errno;
508 512  
509   - if(close_on_exec) {
  513 + if (close_on_exec) {
510 514 err = os_set_exec_close(sock);
511   - if(err < 0)
512   - printk("create_unix_socket : close_on_exec failed, "
513   - "err = %d", -err);
  515 + if (err < 0)
  516 + printk(UM_KERN_ERR "create_unix_socket : "
  517 + "close_on_exec failed, err = %d", -err);
514 518 }
515 519  
516 520 addr.sun_family = AF_UNIX;
517 521  
518   - /* XXX Be more careful about overflow */
519 522 snprintf(addr.sun_path, len, "%s", file);
520 523  
521 524 err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
522   - if(err < 0)
  525 + if (err < 0)
523 526 return -errno;
524 527  
525 528 return sock;
526 529  
527 530  
... ... @@ -540,17 +543,18 @@
540 543 int err, save;
541 544  
542 545 err = fcntl(fd, F_SETLK, &lock);
543   - if(!err)
  546 + if (!err)
544 547 goto out;
545 548  
546 549 save = -errno;
547 550 err = fcntl(fd, F_GETLK, &lock);
548   - if(err){
  551 + if (err) {
549 552 err = -errno;
550 553 goto out;
551 554 }
552 555  
553   - printk("F_SETLK failed, file already locked by pid %d\n", lock.l_pid);
  556 + printk(UM_KERN_ERR "F_SETLK failed, file already locked by pid %d\n",
  557 + lock.l_pid);
554 558 err = save;
555 559 out:
556 560 return err;