~ubuntu-branches/ubuntu/saucy/syslog-ng/saucy

« back to all changes in this revision

Viewing changes to tests/unit/test_thread_wakeup.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2011-11-15 08:48:02 UTC
  • mfrom: (26.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20111115084802-n0jegdnjlxk0m26s
Tags: 3.3.1.dfsg-1ubuntu1
* debian/control: remove libsystemd-daemon-dev build-depends
* debian/rules: remove --with-systemdsystemunitdir from
  override_dh_auto_configure.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <sys/types.h>
 
2
#include <sys/socket.h>
 
3
#include <sys/un.h>
 
4
#include <fcntl.h>
 
5
#include <signal.h>
 
6
#include <pthread.h>
 
7
#include <stdio.h>
 
8
#include <errno.h>
 
9
#include <unistd.h>
 
10
#include <stdlib.h>
 
11
#include <string.h>
 
12
 
 
13
#include <glib.h>
 
14
 
 
15
gboolean thread_exit = FALSE;
 
16
gboolean thread_started;
 
17
GCond *thread_startup;
 
18
GMutex *thread_lock;
 
19
pthread_t thread_handle;
 
20
 
 
21
void
 
22
sigusr1_handler(int signo)
 
23
{
 
24
}
 
25
 
 
26
static void
 
27
setup_sigusr1(void)
 
28
{
 
29
  struct sigaction sa;
 
30
 
 
31
  memset(&sa, 0, sizeof(sa));
 
32
  /* NOTE: this has to be a real signal handler as SIG_IGN will not interrupt the accept call below */
 
33
  sa.sa_handler = sigusr1_handler;
 
34
  sa.sa_flags = 0;
 
35
  sigaction(SIGUSR1, &sa, NULL);
 
36
}
 
37
 
 
38
static void
 
39
signal_startup(void)
 
40
{
 
41
  thread_handle = pthread_self();
 
42
  g_mutex_lock(thread_lock);
 
43
  thread_started = TRUE;
 
44
  g_cond_signal(thread_startup);
 
45
  g_mutex_unlock(thread_lock);
 
46
}
 
47
 
 
48
static gboolean
 
49
create_test_thread(GThreadFunc thread_func, gpointer data)
 
50
{
 
51
  GThread *t;
 
52
  struct timespec nsleep;
 
53
 
 
54
  thread_exit = FALSE;
 
55
  thread_started = FALSE;
 
56
  t = g_thread_create(thread_func, data, TRUE, NULL);
 
57
  g_mutex_lock(thread_lock);
 
58
  while (!thread_started)
 
59
    g_cond_wait(thread_startup, thread_lock);
 
60
  g_mutex_unlock(thread_lock);
 
61
  nsleep.tv_sec = 0;
 
62
  nsleep.tv_nsec = 1e6;
 
63
  nanosleep(&nsleep, NULL);
 
64
  thread_exit = TRUE;
 
65
  pthread_kill(thread_handle, SIGUSR1);
 
66
  return (g_thread_join(t) != NULL);
 
67
}
 
68
 
 
69
int sock;
 
70
 
 
71
gpointer
 
72
accept_thread_func(gpointer args)
 
73
{
 
74
  struct sockaddr_un peer;
 
75
  gint new_sock;
 
76
  gpointer result = NULL;
 
77
 
 
78
  setup_sigusr1();
 
79
  signal_startup();
 
80
  while (1)
 
81
    {
 
82
      gint err;
 
83
      socklen_t peer_size = sizeof(peer);
 
84
 
 
85
      new_sock = accept(sock, (struct sockaddr *) &peer, &peer_size);
 
86
      err = errno;
 
87
 
 
88
      if (new_sock >= 0)
 
89
        close(new_sock);
 
90
      if (thread_exit)
 
91
        {
 
92
          fprintf(stderr, "accept woken up, errno=%s\n", g_strerror(err));
 
93
          result = (gpointer) 0x1;
 
94
          break;
 
95
        }
 
96
    }
 
97
  close(sock);
 
98
  return result;
 
99
}
 
100
 
 
101
int
 
102
test_accept_wakeup()
 
103
{
 
104
  struct sockaddr_un s;
 
105
 
 
106
  unlink("almafa");
 
107
  sock = socket(PF_UNIX, SOCK_STREAM, 0);
 
108
  s.sun_family = AF_UNIX;
 
109
  strcpy(s.sun_path, "almafa");
 
110
  if (bind(sock, (struct sockaddr *) &s, sizeof(s)) < 0)
 
111
    {
 
112
      perror("error binding socket");
 
113
      return 1;
 
114
    }
 
115
  if (listen(sock, 255) < 0)
 
116
    {
 
117
      perror("error in listen()");
 
118
      return 1;
 
119
    }
 
120
  return create_test_thread(accept_thread_func, NULL);
 
121
}
 
122
 
 
123
gpointer
 
124
read_thread_func(gpointer args)
 
125
{
 
126
  gint *pair = (gint *) args;
 
127
  gpointer result = NULL;
 
128
 
 
129
  setup_sigusr1();
 
130
  signal_startup();
 
131
  while (1)
 
132
    {
 
133
      gint err;
 
134
      gchar buf[1024];
 
135
      gint count = 0;
 
136
 
 
137
      count = read(pair[1], buf, sizeof(buf));
 
138
      err = errno;
 
139
 
 
140
      if (thread_exit)
 
141
        {
 
142
          fprintf(stderr, "read woken up, errno=%s\n", g_strerror(err));
 
143
          result = (gpointer) 0x1;
 
144
          break;
 
145
        }
 
146
    }
 
147
  close(pair[0]);
 
148
  close(pair[1]);
 
149
  return result;
 
150
}
 
151
 
 
152
int
 
153
test_read_wakeup()
 
154
{
 
155
  gint pair[2];
 
156
 
 
157
  socketpair(PF_UNIX, SOCK_STREAM, 0, pair);
 
158
  return create_test_thread(read_thread_func, pair);
 
159
}
 
160
 
 
161
int
 
162
main(int argc, char *argv[])
 
163
{
 
164
  g_thread_init(NULL);
 
165
 
 
166
  thread_lock = g_mutex_new();
 
167
  thread_startup = g_cond_new();
 
168
 
 
169
  if (!test_accept_wakeup() ||
 
170
      !test_read_wakeup())
 
171
    return 1;
 
172
  return 0;
 
173
}