1 /* 2 * Copyright (C) 2016 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 #include <stdio.h> 18 #include <stdlib.h> 19 #include <string.h> 20 #include <sys/types.h> 21 #include <unistd.h> 22 23 #define LOG_TAG "dumpstate" 24 #include <cutils/log.h> 25 26 void PrintDefaultOutput() { 27 fprintf(stdout, "stdout\n"); 28 fflush(stdout); 29 fprintf(stderr, "stderr\n"); 30 fflush(stderr); 31 } 32 33 /* 34 * Binary used to on RunCommand tests. 35 * 36 * Usage: 37 * 38 * - Unless stated otherwise this command: 39 * 40 * 1.Prints `stdout\n` on `stdout` and flushes it. 41 * 2.Prints `stderr\n` on `stderr` and flushes it. 42 * 3.Exit with status 0. 43 * 44 * - If 1st argument is '--pid', it first prints its pid on `stdout`. 45 * 46 * - If 1st argument is '--uid', it first prints its uid on `stdout`. 47 * 48 * - If 1st argument is '--crash', it uses ALOGF to crash and returns 666. 49 * 50 * - With argument '--exit' 'CODE', returns CODE; 51 * 52 * - With argument '--sleep 'TIME': 53 * 54 * 1.Prints `stdout line1\n` on `stdout` and `sleeping TIME s\n` on `stderr` 55 * 2.Sleeps for TIME s 56 * 3.Prints `stdout line2\n` on `stdout` and `woke up\n` on `stderr` 57 */ 58 int main(int argc, char* const argv[]) { 59 if (argc == 2) { 60 if (strcmp(argv[1], "--crash") == 0) { 61 PrintDefaultOutput(); 62 LOG_FATAL("D'OH\n"); 63 return 666; 64 } 65 } 66 if (argc == 3) { 67 if (strcmp(argv[1], "--exit") == 0) { 68 PrintDefaultOutput(); 69 return atoi(argv[2]); 70 } 71 } 72 73 if (argc > 1) { 74 int index = 1; 75 76 // First check arguments that can shift the index. 77 if (strcmp(argv[1], "--pid") == 0) { 78 index++; 79 fprintf(stdout, "%d\n", getpid()); 80 fflush(stdout); 81 } else if (strcmp(argv[1], "--uid") == 0) { 82 index++; 83 fprintf(stdout, "%d\n", getuid()); 84 fflush(stdout); 85 } 86 87 // Then the "common" arguments, if any. 88 if (argc > index + 1) { 89 if (strcmp(argv[index], "--sleep") == 0) { 90 int napTime = atoi(argv[index + 1]); 91 fprintf(stdout, "stdout line1\n"); 92 fflush(stdout); 93 fprintf(stderr, "sleeping for %ds\n", napTime); 94 fflush(stderr); 95 sleep(napTime); 96 fprintf(stdout, "stdout line2\n"); 97 fflush(stdout); 98 fprintf(stderr, "woke up\n"); 99 fflush(stderr); 100 return 0; 101 } 102 } 103 } 104 105 PrintDefaultOutput(); 106 return 0; 107 } 108