1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 // This is a simple program used to test interaction with update_engine when
18 // executing other programs. This program receives pre-programmed actions in the
19 // command line and executes them in order.
20 
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 
27 #include <string>
28 
29 #define EX_USAGE_ERROR 100
30 
31 void usage(const char* program, const char* error) {
32   if (error)
33     fprintf(stderr, "ERROR: %s\n", error);
34   fprintf(stderr, "Usage: %s <cmd> [args..]\n", program);
35   exit(EX_USAGE_ERROR);
36 }
37 
38 int main(int argc, char** argv, char** envp) {
39   if (argc < 2)
40     usage(argv[0], "No command passed");
41 
42   std::string cmd(argv[1]);
43   if (cmd == "fstat") {
44     // Call fstat on the passed file descriptor number
45     if (argc < 3)
46       usage(argv[0], "No fd passed to fstat");
47     int fd = atoi(argv[2]);
48     struct stat buf;
49     int rc = fstat(fd, &buf);
50     if (rc < 0) {
51       int ret = errno;
52       perror("fstat");
53       return ret;
54     }
55     return 0;
56   }
57 
58   usage(argv[0], "Unknown command");
59 }
60