Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * XXX This sample code was once meant to show how to use the basic Libevent |
michael@0 | 3 | * interfaces, but it never worked on non-Unix platforms, and some of the |
michael@0 | 4 | * interfaces have changed since it was first written. It should probably |
michael@0 | 5 | * be removed or replaced with something better. |
michael@0 | 6 | * |
michael@0 | 7 | * Compile with: |
michael@0 | 8 | * cc -I/usr/local/include -o event-test event-test.c -L/usr/local/lib -levent |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include <event2/event-config.h> |
michael@0 | 12 | |
michael@0 | 13 | #include <sys/types.h> |
michael@0 | 14 | #include <sys/stat.h> |
michael@0 | 15 | #ifndef WIN32 |
michael@0 | 16 | #include <sys/queue.h> |
michael@0 | 17 | #include <unistd.h> |
michael@0 | 18 | #include <sys/time.h> |
michael@0 | 19 | #else |
michael@0 | 20 | #include <winsock2.h> |
michael@0 | 21 | #include <windows.h> |
michael@0 | 22 | #endif |
michael@0 | 23 | #include <fcntl.h> |
michael@0 | 24 | #include <stdlib.h> |
michael@0 | 25 | #include <stdio.h> |
michael@0 | 26 | #include <string.h> |
michael@0 | 27 | #include <errno.h> |
michael@0 | 28 | |
michael@0 | 29 | #include <event.h> |
michael@0 | 30 | |
michael@0 | 31 | static void |
michael@0 | 32 | fifo_read(evutil_socket_t fd, short event, void *arg) |
michael@0 | 33 | { |
michael@0 | 34 | char buf[255]; |
michael@0 | 35 | int len; |
michael@0 | 36 | struct event *ev = arg; |
michael@0 | 37 | #ifdef WIN32 |
michael@0 | 38 | DWORD dwBytesRead; |
michael@0 | 39 | #endif |
michael@0 | 40 | |
michael@0 | 41 | /* Reschedule this event */ |
michael@0 | 42 | event_add(ev, NULL); |
michael@0 | 43 | |
michael@0 | 44 | fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n", |
michael@0 | 45 | (int)fd, event, arg); |
michael@0 | 46 | #ifdef WIN32 |
michael@0 | 47 | len = ReadFile((HANDLE)fd, buf, sizeof(buf) - 1, &dwBytesRead, NULL); |
michael@0 | 48 | |
michael@0 | 49 | /* Check for end of file. */ |
michael@0 | 50 | if (len && dwBytesRead == 0) { |
michael@0 | 51 | fprintf(stderr, "End Of File"); |
michael@0 | 52 | event_del(ev); |
michael@0 | 53 | return; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | buf[dwBytesRead] = '\0'; |
michael@0 | 57 | #else |
michael@0 | 58 | len = read(fd, buf, sizeof(buf) - 1); |
michael@0 | 59 | |
michael@0 | 60 | if (len == -1) { |
michael@0 | 61 | perror("read"); |
michael@0 | 62 | return; |
michael@0 | 63 | } else if (len == 0) { |
michael@0 | 64 | fprintf(stderr, "Connection closed\n"); |
michael@0 | 65 | return; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | buf[len] = '\0'; |
michael@0 | 69 | #endif |
michael@0 | 70 | fprintf(stdout, "Read: %s\n", buf); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | int |
michael@0 | 74 | main(int argc, char **argv) |
michael@0 | 75 | { |
michael@0 | 76 | struct event evfifo; |
michael@0 | 77 | #ifdef WIN32 |
michael@0 | 78 | HANDLE socket; |
michael@0 | 79 | /* Open a file. */ |
michael@0 | 80 | socket = CreateFileA("test.txt", /* open File */ |
michael@0 | 81 | GENERIC_READ, /* open for reading */ |
michael@0 | 82 | 0, /* do not share */ |
michael@0 | 83 | NULL, /* no security */ |
michael@0 | 84 | OPEN_EXISTING, /* existing file only */ |
michael@0 | 85 | FILE_ATTRIBUTE_NORMAL, /* normal file */ |
michael@0 | 86 | NULL); /* no attr. template */ |
michael@0 | 87 | |
michael@0 | 88 | if (socket == INVALID_HANDLE_VALUE) |
michael@0 | 89 | return 1; |
michael@0 | 90 | |
michael@0 | 91 | #else |
michael@0 | 92 | struct stat st; |
michael@0 | 93 | const char *fifo = "event.fifo"; |
michael@0 | 94 | int socket; |
michael@0 | 95 | |
michael@0 | 96 | if (lstat(fifo, &st) == 0) { |
michael@0 | 97 | if ((st.st_mode & S_IFMT) == S_IFREG) { |
michael@0 | 98 | errno = EEXIST; |
michael@0 | 99 | perror("lstat"); |
michael@0 | 100 | exit(1); |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | unlink(fifo); |
michael@0 | 105 | if (mkfifo(fifo, 0600) == -1) { |
michael@0 | 106 | perror("mkfifo"); |
michael@0 | 107 | exit(1); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | /* Linux pipes are broken, we need O_RDWR instead of O_RDONLY */ |
michael@0 | 111 | #ifdef __linux |
michael@0 | 112 | socket = open(fifo, O_RDWR | O_NONBLOCK, 0); |
michael@0 | 113 | #else |
michael@0 | 114 | socket = open(fifo, O_RDONLY | O_NONBLOCK, 0); |
michael@0 | 115 | #endif |
michael@0 | 116 | |
michael@0 | 117 | if (socket == -1) { |
michael@0 | 118 | perror("open"); |
michael@0 | 119 | exit(1); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | fprintf(stderr, "Write data to %s\n", fifo); |
michael@0 | 123 | #endif |
michael@0 | 124 | /* Initalize the event library */ |
michael@0 | 125 | event_init(); |
michael@0 | 126 | |
michael@0 | 127 | /* Initalize one event */ |
michael@0 | 128 | #ifdef WIN32 |
michael@0 | 129 | event_set(&evfifo, (evutil_socket_t)socket, EV_READ, fifo_read, &evfifo); |
michael@0 | 130 | #else |
michael@0 | 131 | event_set(&evfifo, socket, EV_READ, fifo_read, &evfifo); |
michael@0 | 132 | #endif |
michael@0 | 133 | |
michael@0 | 134 | /* Add it to the active events, without a timeout */ |
michael@0 | 135 | event_add(&evfifo, NULL); |
michael@0 | 136 | |
michael@0 | 137 | event_dispatch(); |
michael@0 | 138 | #ifdef WIN32 |
michael@0 | 139 | CloseHandle(socket); |
michael@0 | 140 | #endif |
michael@0 | 141 | return (0); |
michael@0 | 142 | } |
michael@0 | 143 |