| 1 | |
|---|
| 2 | #include <usrint.h> |
|---|
| 3 | #include "ucached.h" |
|---|
| 4 | |
|---|
| 5 | /* |
|---|
| 6 | * s = start ucached |
|---|
| 7 | * c = create shared memory for ucache |
|---|
| 8 | * d = destroy shared memory for ucache |
|---|
| 9 | * x = exit ucached |
|---|
| 10 | */ |
|---|
| 11 | int main(int argc, char **argv) |
|---|
| 12 | { |
|---|
| 13 | if(argc < 2 || argc > 3) |
|---|
| 14 | { |
|---|
| 15 | printf("usage: ucache_cmd <command char>\n"); |
|---|
| 16 | return 0; |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | int rc = 0; |
|---|
| 20 | void *rp; |
|---|
| 21 | |
|---|
| 22 | char this_cmd = argv[1][0]; |
|---|
| 23 | if(this_cmd == 's') |
|---|
| 24 | { |
|---|
| 25 | char ps_buff[256]; |
|---|
| 26 | FILE *pipe = popen("ps -e | grep -w ucached", "r"); |
|---|
| 27 | rp = fgets(ps_buff, 256, pipe); |
|---|
| 28 | if(rp == NULL) |
|---|
| 29 | { |
|---|
| 30 | rc = remove(FIFO1); |
|---|
| 31 | rc = remove(FIFO2); |
|---|
| 32 | /* Crank up the daemon since it's not running */ |
|---|
| 33 | rc = system("ucached"); |
|---|
| 34 | puts("SUCCESS: Daemon started"); |
|---|
| 35 | } |
|---|
| 36 | else |
|---|
| 37 | { |
|---|
| 38 | puts("FAILURE: Daemon already started"); |
|---|
| 39 | puts(ps_buff); |
|---|
| 40 | } |
|---|
| 41 | return 1; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | char buffer[BUFF_SIZE]; |
|---|
| 45 | memset(buffer, 0, BUFF_SIZE); |
|---|
| 46 | |
|---|
| 47 | /* Read and Write File Descriptors */ |
|---|
| 48 | int readfd; |
|---|
| 49 | int writefd; |
|---|
| 50 | |
|---|
| 51 | /* Open FIFOs for use */ |
|---|
| 52 | writefd = open(FIFO1, O_WRONLY); |
|---|
| 53 | |
|---|
| 54 | if(writefd == -1) |
|---|
| 55 | { |
|---|
| 56 | perror("ucached_cmd couldn't open writefd"); |
|---|
| 57 | return -1; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | /* Send Command to Daemon */ |
|---|
| 61 | buffer[0] = this_cmd; |
|---|
| 62 | if(argc == 3) |
|---|
| 63 | { |
|---|
| 64 | strcat(buffer, " "); |
|---|
| 65 | strcat(buffer, argv[2]); |
|---|
| 66 | } |
|---|
| 67 | rc = write(writefd, buffer, BUFF_SIZE); |
|---|
| 68 | if(rc == -1) |
|---|
| 69 | { |
|---|
| 70 | perror("Error occured during write to ucached"); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | memset(buffer, 0, BUFF_SIZE); |
|---|
| 74 | readfd = open(FIFO2, O_RDONLY); |
|---|
| 75 | |
|---|
| 76 | /* Collect Response */ |
|---|
| 77 | int count = read(readfd, buffer, BUFF_SIZE); |
|---|
| 78 | while(count > 0 || ((count == -1) && (errno == EINTR))) |
|---|
| 79 | { |
|---|
| 80 | //if(count) |
|---|
| 81 | // printf("read: %d\n", count); |
|---|
| 82 | //buffer[count] = 0; |
|---|
| 83 | fputs(buffer, stdout); |
|---|
| 84 | if(strlen(buffer) < BUFF_SIZE) |
|---|
| 85 | { |
|---|
| 86 | //printf("strlen = %d\n", strlen(buffer)); |
|---|
| 87 | break; |
|---|
| 88 | } |
|---|
| 89 | memset(buffer, 0, BUFF_SIZE); |
|---|
| 90 | count = read(readfd, buffer, BUFF_SIZE); |
|---|
| 91 | } |
|---|
| 92 | printf("\n"); |
|---|
| 93 | /* Close FIFO when done */ |
|---|
| 94 | close(readfd); |
|---|
| 95 | close(writefd); |
|---|
| 96 | |
|---|
| 97 | if(this_cmd == 'i') |
|---|
| 98 | { |
|---|
| 99 | memset(buffer, 0, BUFF_SIZE); |
|---|
| 100 | FILE *info = fopen(UCACHED_INFO_FILE, "r"); |
|---|
| 101 | /* |
|---|
| 102 | while(!info) |
|---|
| 103 | { |
|---|
| 104 | info = fopen(UCACHED_INFO_FILE, "r"); |
|---|
| 105 | }*/ |
|---|
| 106 | if(!info) |
|---|
| 107 | { |
|---|
| 108 | perror("UCACHED_INFO_FILE"); |
|---|
| 109 | } |
|---|
| 110 | while(fread(buffer, sizeof(char), BUFF_SIZE - 1, info) > 0) |
|---|
| 111 | { |
|---|
| 112 | buffer[strlen(buffer)] = 0; |
|---|
| 113 | printf("%s", buffer); |
|---|
| 114 | memset(buffer, 0, BUFF_SIZE); |
|---|
| 115 | } |
|---|
| 116 | fclose(info); |
|---|
| 117 | } |
|---|
| 118 | return 1; |
|---|
| 119 | } |
|---|
| 120 | |
|---|