Code Coverage: vinyl-cache/bin/vinyltest/vtest2/src/vtc_sess.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2020 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 */
30
31
#include "config.h"
32
33
#include <stdlib.h>
34
#include <string.h>
35
36
#include "vtc.h"
37
#include "vtc_http.h"
38
39
struct thread_arg {
40
        unsigned                magic;
41
#define THREAD_ARG_MAGIC        0xd5dc5f1c
42
        void                    *priv;
43
        sess_conn_f             *conn_f;
44
        sess_disc_f             *disc_f;
45
        const char              *listen_addr;
46
        struct vtc_sess         *vsp;
47
        int                     *asocket;
48
        const char              *spec;
49
};
50
51
struct vtc_sess *
52
Sess_New(struct vtclog *vl, const char *name)
53 2546
{
54 2546
        struct vtc_sess *vsp;
55
56 2546
        ALLOC_OBJ(vsp, VTC_SESS_MAGIC);
57 2546
        AN(vsp);
58 2546
        vsp->vl = vl;
59 2546
        REPLACE(vsp->name, name);
60 2546
        vsp->repeat = 1;
61 2546
        return (vsp);
62 2546
}
63
64
void
65
Sess_Destroy(struct vtc_sess **vspp)
66 2546
{
67 2546
        struct vtc_sess *vsp;
68
69 2546
        TAKE_OBJ_NOTNULL(vsp, vspp, VTC_SESS_MAGIC);
70 2546
        REPLACE(vsp->name, NULL);
71 2546
        FREE_OBJ(vsp);
72 2546
}
73
74
int
75
Sess_GetOpt(struct vtc_sess *vsp, char * const **avp)
76 6706
{
77 6706
        char * const *av;
78 6706
        int rv = 0;
79
80 6706
        CHECK_OBJ_NOTNULL(vsp, VTC_SESS_MAGIC);
81 6706
        AN(avp);
82 6706
        av = *avp;
83 6706
        AN(*av);
84 6706
        if (!vstrcmp(*av, "-rcvbuf")) {
85 8
                AN(av[1]);
86 8
                vsp->rcvbuf = atoi(av[1]);
87 8
                av += 1;
88 8
                rv = 1;
89 6698
        } else if (!vstrcmp(*av, "-repeat")) {
90 108
                AN(av[1]);
91 108
                vsp->repeat = atoi(av[1]);
92 108
                av += 1;
93 108
                rv = 1;
94 6590
        } else if (!vstrcmp(*av, "-keepalive")) {
95 34
                vsp->keepalive = 1;
96 34
                rv = 1;
97 34
        }
98 6706
        *avp = av;
99 6706
        return (rv);
100 6706
}
101
102
void
103
sess_process(struct vtclog *vl, struct vtc_sess *vsp,
104
    const char *spec, int *sock, int *sfd, const char *addr)
105 4440
{
106
107 4440
        CHECK_OBJ_NOTNULL(vsp, VTC_SESS_MAGIC);
108
109 4440
        http_process(vl, vsp, spec, sock, sfd, addr, vsp->rcvbuf);
110 4440
}
111
112
static void *
113
sess_thread(void *priv)
114 3169
{
115 3169
        struct vtclog *vl;
116 3169
        struct vtc_sess *vsp;
117 3169
        struct thread_arg ta, *tap;
118 3169
        int i, fd = -1;
119
120 3169
        CAST_OBJ_NOTNULL(tap, priv, THREAD_ARG_MAGIC);
121 3169
        ta = *tap;
122 3169
        FREE_OBJ(tap);
123
124 3169
        vsp = ta.vsp;
125 3169
        CHECK_OBJ_NOTNULL(vsp, VTC_SESS_MAGIC);
126 3169
        vl = vtc_logopen("%s", vsp->name);
127 3169
        pthread_cleanup_push(vtc_logclose, vl);
128
129 3169
        assert(vsp->repeat > 0);
130 3169
        vtc_log(vl, 2, "Started on %s (%u iterations%s)", ta.listen_addr,
131 3169
                vsp->repeat, vsp->keepalive ? " using keepalive" : "");
132 7620
        for (i = 0; i < vsp->repeat; i++) {
133 4451
                if (fd < 0)
134 3992
                        fd = ta.conn_f(ta.priv, vl);
135 4451
                sess_process(vl, ta.vsp, ta.spec, &fd,
136 4451
                    ta.asocket, ta.listen_addr);
137 4451
                if (! vsp->keepalive)
138 3816
                        ta.disc_f(ta.priv, vl, &fd);
139 4451
        }
140 3169
        if (vsp->keepalive)
141 39
                ta.disc_f(ta.priv, vl, &fd);
142 3169
        vtc_log(vl, 2, "Ending");
143 3169
        pthread_cleanup_pop(0);
144 3169
        vtc_logclose(vl);
145 3169
        return (NULL);
146 3169
}
147
148
pthread_t
149
Sess_Start_Thread(
150
    void *priv,
151
    struct vtc_sess *vsp,
152
    sess_conn_f *conn,
153
    sess_disc_f *disc,
154
    const char *listen_addr,
155
    int *asocket,
156
    const char *spec
157
)
158 3169
{
159 3169
        struct thread_arg *ta;
160 3169
        pthread_t pt;
161
162 3169
        AN(priv);
163 3169
        CHECK_OBJ_NOTNULL(vsp, VTC_SESS_MAGIC);
164 3169
        AN(conn);
165 3169
        AN(disc);
166 3169
        AN(listen_addr);
167 3169
        ALLOC_OBJ(ta, THREAD_ARG_MAGIC);
168 3169
        AN(ta);
169 3169
        ta->priv = priv;
170 3169
        ta->vsp = vsp;
171
172 3169
        ta->conn_f = conn;
173 3169
        ta->disc_f = disc;
174 3169
        ta->listen_addr = listen_addr;
175 3169
        ta->asocket = asocket;
176 3169
        ta->spec = spec;
177 3169
        PTOK(pthread_create(&pt, NULL, sess_thread, ta));
178 3169
        return (pt);
179 3169
}