~ubuntu-branches/ubuntu/trusty/erlang/trusty

« back to all changes in this revision

Viewing changes to lib/erl_interface/test/ei_accept_SUITE_data/eiaccnode.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2011-05-05 15:48:43 UTC
  • mfrom: (3.5.13 sid)
  • Revision ID: james.westby@ubuntu.com-20110505154843-0om6ekzg6m7ugj27
Tags: 1:14.b.2-dfsg-3ubuntu1
* Merge from debian unstable.  Remaining changes:
  - Drop libwxgtk2.8-dev build dependency. Wx isn't in main, and not
    supposed to.
  - Drop erlang-wx binary.
  - Drop erlang-wx dependency from -megaco, -common-test, and -reltool, they
    do not really need wx. Also drop it from -debugger; the GUI needs wx,
    but it apparently has CLI bits as well, and is also needed by -megaco,
    so let's keep the package for now.
  - debian/patches/series: Do what I meant, and enable build-options.patch
    instead.
* Additional changes:
  - Drop erlang-wx from -et
* Dropped Changes:
  - patches/pcre-crash.patch: CVE-2008-2371: outer level option with
    alternatives caused crash. (Applied Upstream)
  - fix for ssl certificate verification in newSSL: 
    ssl_cacertfile_fix.patch (Applied Upstream)
  - debian/patches/series: Enable native.patch again, to get stripped beam
    files and reduce the package size again. (build-options is what
    actually accomplished this)
  - Remove build-options.patch on advice from upstream and because it caused
    odd build failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * %CopyrightBegin%
 
3
 * 
 
4
 * Copyright Ericsson AB 2001-2009. All Rights Reserved.
 
5
 * 
 
6
 * The contents of this file are subject to the Erlang Public License,
 
7
 * Version 1.1, (the "License"); you may not use this file except in
 
8
 * compliance with the License. You should have received a copy of the
 
9
 * Erlang Public License along with this software. If not, it can be
 
10
 * retrieved online at http://www.erlang.org/.
 
11
 * 
 
12
 * Software distributed under the License is distributed on an "AS IS"
 
13
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 
14
 * the License for the specific language governing rights and limitations
 
15
 * under the License.
 
16
 * 
 
17
 * %CopyrightEnd%
 
18
 */
 
19
 
 
20
/* to test multiple threads in ei */
 
21
 
 
22
#include <stdlib.h>
 
23
#include <stdio.h>
 
24
 
 
25
#ifdef __WIN32__
 
26
#include <winsock2.h>
 
27
#include <windows.h>
 
28
#include <process.h>
 
29
#else
 
30
#ifndef VXWORKS
 
31
#include <pthread.h>
 
32
#endif
 
33
#include <sys/types.h>
 
34
#include <sys/socket.h>
 
35
#include <netinet/in.h>
 
36
#endif
 
37
 
 
38
#include "ei.h"
 
39
 
 
40
#ifdef VXWORKS
 
41
#include <vxWorks.h>
 
42
#include <sockLib.h>
 
43
#include <inetLib.h>
 
44
#define MAIN cnode
 
45
#else
 
46
#define MAIN main
 
47
#endif
 
48
 
 
49
static int my_listen(int port);
 
50
 
 
51
/*
 
52
   A small einode.
 
53
   To be called from the test case ei_accept_SUITE:multi_thread
 
54
   usage: eiaccnode <cookie> <n>
 
55
 
 
56
   - start threads 0..n-1
 
57
   - in each thread
 
58
      - listen on "ei0" .. "ei<n-1>"
 
59
      - wait for connection 
 
60
      - receive a pid
 
61
      - send {i, <pid>} back
 
62
      - shutdown gracefully
 
63
*/
 
64
 
 
65
static const char* cookie, * desthost;
 
66
static int port; /* actually base port */
 
67
 
 
68
#ifndef SD_SEND
 
69
#ifdef SHUTWR
 
70
#define SD_SEND SHUT_WR
 
71
#else
 
72
#define SD_SEND 1
 
73
#endif
 
74
#endif
 
75
 
 
76
#ifndef __WIN32__
 
77
#define closesocket(fd) close(fd)
 
78
#endif
 
79
 
 
80
#ifdef __WIN32__
 
81
static DWORD WINAPI
 
82
#else
 
83
static void*
 
84
#endif
 
85
    einode_thread(void* num)
 
86
{
 
87
    int n = (int)num;
 
88
    ei_cnode ec;
 
89
    char myname[100], destname[100];
 
90
    int r, fd, listen;
 
91
    ErlConnect conn;
 
92
    erlang_msg msg;
 
93
/*    FILE* f;*/
 
94
 
 
95
    sprintf(myname, "eiacc%d", n);
 
96
    printf("thread %d (%s) listening\n", n, myname, destname);
 
97
    r = ei_connect_init(&ec, myname, cookie, 0);
 
98
    if ((listen = my_listen(port+n)) <= 0) {
 
99
        printf("listen err\n");
 
100
        exit(7);
 
101
    }
 
102
    if (ei_publish(&ec, port + n) == -1) {
 
103
        printf("ei_publish port %d\n", port+n);
 
104
        exit(8);
 
105
    }
 
106
    fd = ei_accept(&ec, listen, &conn);
 
107
    printf("ei_accept %d\n", fd);
 
108
    if (fd >= 0) {
 
109
        ei_x_buff x, xs;
 
110
        int index, version;
 
111
        erlang_pid pid;
 
112
 
 
113
        ei_x_new(&x);
 
114
        for (;;) {
 
115
            int got = ei_xreceive_msg(fd, &msg, &x);
 
116
            if (got == ERL_TICK)
 
117
                continue;
 
118
            if (got == ERL_ERROR) {
 
119
                printf("receive error %d\n", n);
 
120
                return 0;
 
121
            }
 
122
            printf("received %d\n", got);
 
123
            break;
 
124
        }
 
125
        index = 0;
 
126
        if (ei_decode_version(x.buff, &index, &version) != 0) {
 
127
            printf("ei_decode_version %d\n", n);
 
128
            return 0;
 
129
        }
 
130
        if (ei_decode_pid(x.buff, &index, &pid) != 0) {
 
131
            printf("ei_decode_pid %d\n", n);
 
132
            return 0;
 
133
        }
 
134
/*      fprintf(f, "got pid from %s \n", pid.node);*/
 
135
        ei_x_new_with_version(&xs);
 
136
        ei_x_encode_tuple_header(&xs, 2);
 
137
        ei_x_encode_long(&xs, n);
 
138
        ei_x_encode_pid(&xs, &pid);
 
139
        r = ei_send(fd, &pid, xs.buff, xs.index);
 
140
/*      fprintf(f, "sent %d bytes %d\n", xs.index, r);*/
 
141
        shutdown(fd, SD_SEND);
 
142
        closesocket(fd);
 
143
        ei_x_free(&x);
 
144
        ei_x_free(&xs);
 
145
    } else {
 
146
        printf("coudn't connect fd %d r %d\n", fd, r);
 
147
    }
 
148
    printf("done thread %d\n", n);
 
149
/*    fclose(f);*/
 
150
    return 0;
 
151
}
 
152
 
 
153
MAIN(int argc, char *argv[])
 
154
{
 
155
    int i, n, no_threads;
 
156
#ifndef VXWORKS
 
157
#ifdef __WIN32__
 
158
    HANDLE threads[100];
 
159
#else
 
160
    pthread_t threads[100];
 
161
#endif
 
162
#endif
 
163
 
 
164
    if (argc < 3)
 
165
        exit(1);
 
166
 
 
167
    cookie = argv[1];
 
168
    n = atoi(argv[2]);
 
169
    if (n > 100)
 
170
        exit(2);
 
171
    desthost = argv[3];
 
172
    port = atoi(argv[4]);
 
173
#ifndef VXWORKS    
 
174
    no_threads = argv[5] != NULL && strcmp(argv[5], "nothreads") == 0;
 
175
#else
 
176
    no_threads = 1;
 
177
#endif
 
178
    for (i = 0; i < n; ++i) {
 
179
        if (!no_threads) {
 
180
#ifndef VXWORKS
 
181
#ifdef __WIN32__
 
182
            unsigned tid;
 
183
            threads[i] = (HANDLE)_beginthreadex(NULL, 0, einode_thread,
 
184
                                                (void*)i, 0, &tid);
 
185
#else
 
186
            pthread_create(&threads[i], NULL, einode_thread, (void*)i);
 
187
#endif
 
188
#else
 
189
            ;
 
190
#endif
 
191
        } else
 
192
            einode_thread((void*)i);
 
193
    }
 
194
 
 
195
    if (!no_threads)
 
196
#ifndef VXWORKS
 
197
        for (i = 0; i < n; ++i) {
 
198
#ifdef __WIN32__
 
199
            if (WaitForSingleObject(threads[i], INFINITE) != WAIT_OBJECT_0)
 
200
#else
 
201
            if (pthread_join(threads[i], NULL) != 0)
 
202
#endif
 
203
                printf("bad wait thread %d\n", i);
 
204
        }
 
205
#else
 
206
    ;
 
207
#endif
 
208
    printf("ok\n");
 
209
    return 0;
 
210
}
 
211
 
 
212
static int my_listen(int port)
 
213
{
 
214
    int listen_fd;
 
215
    struct sockaddr_in addr;
 
216
    const char *on = "1";
 
217
    
 
218
    if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
 
219
        return -1;
 
220
    
 
221
    setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, on, sizeof(on));
 
222
    
 
223
    memset((void*) &addr, 0, (size_t) sizeof(addr));
 
224
    addr.sin_family = AF_INET;
 
225
    addr.sin_port = htons(port);
 
226
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
 
227
    
 
228
    if (bind(listen_fd, (struct sockaddr*) &addr, sizeof(addr)) < 0)
 
229
        return -1;
 
230
 
 
231
    listen(listen_fd, 5);
 
232
    return listen_fd;
 
233
}
 
234