• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <asm/unistd.h>
10 
usage(const char * comm)11 void usage(const char *comm) {
12   fprintf(stderr, "Usage: %s <access mode>\n", comm);
13   fprintf(stderr, "\tAccess mode: 0-O_RDONLY, 1-O_WRONLY, 2-O_RDWR\n");
14   return;
15 }
16 
main(int argc,char ** argv)17 int main(int argc, char **argv) {
18   if (argc < 2) {
19     usage(argv[0]);
20     return 1;
21   }
22 
23   unsigned int access_mode = strtoul(argv[1], NULL, 0);
24   if (access_mode < 0 || access_mode > 2) {
25     usage(argv[0]);
26     return 1;
27   }
28 
29   char *path;
30   int flags;
31 
32   switch (access_mode) {
33     case 0:
34       path = "/dev/zero";
35       flags = O_RDONLY;
36       break;
37     case 1:
38       path = "/dev/null";
39       flags = O_WRONLY;
40       break;
41     case 2:
42       path = "/dev/null";
43       flags = O_RDWR;
44       break;
45     default:
46       usage(argv[0]);
47       return 1;
48   }
49 
50   int fd = syscall(__NR_open, path, flags);
51   syscall(__NR_close, fd);
52   syscall(__NR_exit, 0);
53 }
54