test_subprocess.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // This is a simple program used to test interaction with update_engine when
  17. // executing other programs. This program receives pre-programmed actions in the
  18. // command line and executes them in order.
  19. #include <errno.h>
  20. #include <stdlib.h>
  21. #include <sys/stat.h>
  22. #include <sys/types.h>
  23. #include <unistd.h>
  24. #include <string>
  25. #define EX_USAGE_ERROR 100
  26. void usage(const char* program, const char* error) {
  27. if (error)
  28. fprintf(stderr, "ERROR: %s\n", error);
  29. fprintf(stderr, "Usage: %s <cmd> [args..]\n", program);
  30. exit(EX_USAGE_ERROR);
  31. }
  32. int main(int argc, char** argv, char** envp) {
  33. if (argc < 2)
  34. usage(argv[0], "No command passed");
  35. std::string cmd(argv[1]);
  36. if (cmd == "fstat") {
  37. // Call fstat on the passed file descriptor number
  38. if (argc < 3)
  39. usage(argv[0], "No fd passed to fstat");
  40. int fd = atoi(argv[2]);
  41. struct stat buf;
  42. int rc = fstat(fd, &buf);
  43. if (rc < 0) {
  44. int ret = errno;
  45. perror("fstat");
  46. return ret;
  47. }
  48. return 0;
  49. }
  50. usage(argv[0], "Unknown command");
  51. }