San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
| 19 | #include <string.h> |
| 20 | #include <signal.h> |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | |
| 24 | #include <sys/socket.h> |
| 25 | #include <sys/select.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/un.h> |
| 29 | |
| 30 | #include <cutils/sockets.h> |
| 31 | |
| 32 | #include <private/android_filesystem_config.h> |
| 33 | |
| 34 | static void signal_handler(int sig) { |
| 35 | fprintf(stdout, "{ interrupt! }\n"); |
| 36 | } |
| 37 | |
| 38 | int main(int argc, char **argv) { |
| 39 | int sock; |
| 40 | |
| 41 | if ((sock = socket_local_client("nexus", |
| 42 | ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 43 | SOCK_STREAM)) < 0) { |
| 44 | fprintf(stderr, "Error connecting (%s)\n", strerror(errno)); |
| 45 | exit(1); |
| 46 | } |
| 47 | |
| 48 | printf("Connected to nexus\n"); |
| 49 | |
| 50 | while(1) { |
| 51 | fd_set read_fds; |
| 52 | struct timeval to; |
| 53 | int rc = 0; |
| 54 | |
| 55 | signal(SIGINT, SIG_DFL); |
| 56 | |
| 57 | printf("-> "); |
| 58 | fflush(stdout); |
| 59 | |
| 60 | char buffer[255]; |
| 61 | if (!fgets(buffer, sizeof(buffer) -1, stdin)) { |
| 62 | printf("Exiting...\n"); |
| 63 | exit(0); |
| 64 | } |
| 65 | |
| 66 | buffer[strlen(buffer) -1] = 0; |
| 67 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 68 | if (write(sock, buffer, strlen(buffer) +1) < 0) { |
| 69 | fprintf(stderr, "Error writing data (%s)\n", strerror(errno)); |
| 70 | exit(2); |
| 71 | } |
| 72 | |
| 73 | wait: |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame^] | 74 | to.tv_sec = 10; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 75 | to.tv_usec = 0; |
| 76 | FD_ZERO(&read_fds); |
| 77 | FD_SET(sock, &read_fds); |
| 78 | |
| 79 | signal(SIGINT, signal_handler); |
| 80 | |
| 81 | if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) { |
| 82 | if (errno == EINTR) |
| 83 | continue; |
| 84 | fprintf(stderr, "Error in select (%s)\n", strerror(errno)); |
| 85 | exit(2); |
| 86 | } else if (!rc) { |
| 87 | printf("{response timeout}\n"); |
| 88 | continue; |
| 89 | } else if (FD_ISSET(sock, &read_fds)) { |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame^] | 90 | if ((rc = read(sock, buffer, sizeof(buffer)-1)) <= 0) { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 91 | fprintf(stderr, "Error reading response (%s)\n", strerror(errno)); |
| 92 | exit(2); |
| 93 | } |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame^] | 94 | printf(" %s\n", buffer); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 95 | goto wait; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | exit(0); |
| 101 | |
| 102 | } |