Skip to content
Browse files

Bug 1251: exiting when stdin closes

  • Loading branch information...
1 parent 17129c9 commit c7d7466ac5f20f9f92f578adda7ae04674230d88 @steveyen steveyen committed Jun 9, 2010
Showing with 45 additions and 2 deletions.
  1. +2 −1 Makefile.am
  2. +7 −1 memcached.c
  3. +30 −0 stdin_check.c
  4. +6 −0 stdin_check.h
View
3 Makefile.am
@@ -35,7 +35,8 @@ moxi_SOURCES = memcached.c memcached.h \
cproxy_front.c \
matcher.c matcher.h \
murmur_hash.c \
- mcs.c mcs.h
+ mcs.c mcs.h \
+ stdin_check.c stdin_check.h
timedrun_SOURCES = timedrun.c
View
8 memcached.c
@@ -48,6 +48,7 @@
#include <stddef.h>
#include "cproxy.h"
+#include "stdin_check.h"
/* FreeBSD 4.x doesn't have IOV_MAX exposed. */
#ifndef IOV_MAX
@@ -4560,8 +4561,13 @@ int main (int argc, char **argv) {
#endif
}
- if (cproxy_behavior)
+#ifndef MAIN_CHECK
+ stdin_check();
+#endif
+
+ if (cproxy_behavior) {
free(cproxy_behavior);
+ }
/* Drop privileges no longer needed */
drop_privileges();
View
30 stdin_check.c
@@ -0,0 +1,30 @@
+/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+
+#include "stdin_check.h"
+
+static void* check_stdin_thread(void* arg)
+{
+ pthread_detach(pthread_self());
+
+ while (!feof(stdin)) {
+ getc(stdin);
+ }
+
+ fprintf(stderr, "EOF on stdin. Exiting\n");
+ exit(0);
+ /* NOTREACHED */
+ return NULL;
+}
+
+int stdin_check(void) {
+ pthread_t t;
+ if (pthread_create(&t, NULL, check_stdin_thread, NULL) != 0) {
+ perror("couldn't create stdin checking thread.");
+ return -1;
+ }
+
+ return 0;
+}
View
6 stdin_check.h
@@ -0,0 +1,6 @@
+#ifndef STDIN_CHECK_H
+#define STDIN_CHECK_H
+
+int stdin_check(void);
+
+#endif

0 comments on commit c7d7466

Please sign in to comment.
Something went wrong with that request. Please try again.