aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/test/trace_SUITE_data/slow_drv.c
blob: c3e9db0fb8edb961ae80173f7fcd3a22a07c294b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* ``The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved via the world wide web at http://www.erlang.org/.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Initial Developer of the Original Code is Ericsson Utvecklings AB.
 * Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
 * AB. All Rights Reserved.''
 * 
 *     $Id$
 */

#ifndef UNIX
#if !defined(__WIN32__) && !defined(VXWORKS)
#define UNIX 1
#endif
#endif

/* We actually only want to run this on 
   Unix machines which have the usleep call */
#if defined(UNIX) && !defined(HAVE_USLEEP)
#undef UNIX
#endif

#ifdef UNIX
#include <errno.h>
#include <stdio.h>
#include <stdlib.h> /* rand */
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#ifdef HAVE_POLL_H
#  include <poll.h>
#endif
#endif /* UNIX */

#include "erl_driver.h"

static int slow_drv_init(void);
static void slow_drv_finish(void);
static ErlDrvData slow_drv_start(ErlDrvPort, char *);
static void slow_drv_stop(ErlDrvData);
static void slow_drv_ready_input(ErlDrvData, ErlDrvEvent);
static ErlDrvSSizeT slow_drv_control(ErlDrvData, unsigned int,
				      char *, ErlDrvSizeT, char **, ErlDrvSizeT);

static ErlDrvEntry slow_drv_entry = { 
    slow_drv_init,
    slow_drv_start,
    slow_drv_stop,
    NULL, /* output */
    slow_drv_ready_input,
    NULL, /* ready_output */
    "slow_drv",
    slow_drv_finish,
    NULL, /* handle */
    slow_drv_control,
    NULL, /* timeout */
    NULL, /* outputv */
    NULL, /* ready_async */
    NULL, /* flush */
    NULL, /* call */
    NULL, /* event */

    ERL_DRV_EXTENDED_MARKER,
    ERL_DRV_EXTENDED_MAJOR_VERSION,
    ERL_DRV_EXTENDED_MINOR_VERSION,
    ERL_DRV_FLAG_USE_PORT_LOCKING,
    NULL,/* void *handle2 */
    NULL,/* process_exit */
    NULL /* stop select */
};
typedef struct {
    ErlDrvPort port;
    ErlDrvTermData id;
    int test;
    int s[2];
} SlowDrvData;

/* -------------------------------------------------------------------------
** Entry functions
**/

DRIVER_INIT(slow_drv)
{
    return &slow_drv_entry;
}


static int
slow_drv_init(void)
{
    return 0;
}

static void
slow_drv_finish(void)
{
}


static ErlDrvData
slow_drv_start(ErlDrvPort port, char *command)
{
#ifndef UNIX
    return NULL;
#else
    SlowDrvData *sddp = driver_alloc(sizeof(SlowDrvData));
    if (!sddp) {
	errno = ENOMEM;
	return ERL_DRV_ERROR_ERRNO;
    }
    sddp->port = port;
    sddp->id = driver_mk_port(port);
    sddp->test = 0;
    sddp->s[0] = sddp->s[1] = -1;
    return (ErlDrvData) sddp;
#endif
}

static void
slow_drv_stop(ErlDrvData drv_data) {
#ifdef UNIX
    SlowDrvData *sddp = (SlowDrvData *) drv_data;

    if (sddp->test) {
	driver_select(sddp->port, (ErlDrvEvent) (ErlDrvSInt) sddp->s[0], DO_READ, 0);
	close(sddp->s[0]);
	close(sddp->s[1]);
    }

    driver_free((void *) sddp);

#endif
}

static ErlDrvSSizeT
slow_drv_control(ErlDrvData drv_data,
		 unsigned int command,
		 char *buf, ErlDrvSizeT len,
		 char **rbuf, ErlDrvSizeT rlen)
{
    SlowDrvData *sddp = (SlowDrvData *) drv_data;
    char *res_str = "ok";
    int res_len;
    switch (command) {
    case 0:
	/* just be slow */
	usleep(222000);
	break;
    case 1:
	/* create pipes and select on input */
	if (sddp->test) {
	    res_str = "no";
	    break;
	}
	sddp->test = 1;
	pipe(sddp->s);
	driver_select(sddp->port, (ErlDrvEvent) (ErlDrvSInt) sddp->s[0], DO_READ, 1);
	break;
    case 2:
	if (!sddp->test) {
	    res_str = "no";
	    break;
	}
	write(sddp->s[1],"boo",3);
	break;
    }

    res_len = strlen(res_str);
    if (res_len > rlen) {
	char *abuf = driver_alloc(sizeof(char)*res_len);
	if (!abuf)
	    return 0;
	*rbuf = abuf;
    }

    memcpy((void *) *rbuf, (void *) res_str, res_len);

    return res_len;
}

static void
slow_drv_ready_input(ErlDrvData drv_data, ErlDrvEvent event)
{
#ifdef UNIX
    SlowDrvData *sddp = (SlowDrvData *) drv_data;
    int fd = (int) (ErlDrvSInt) event;
    if (sddp->test) {
	char buff[3];
	usleep(212000);
	read(sddp->s[0],buff,3);
	driver_output(sddp->port, "TheEnd", 6);
    }

#endif
}