Annotation of embedaddon/strongswan/src/libcharon/plugins/load_tester/load_tester.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2012 Martin Willi
                      3:  * Copyright (C) 2012 revosec AG
                      4:  *
                      5:  * This program is free software; you can redistribute it and/or modify it
                      6:  * under the terms of the GNU General Public License as published by the
                      7:  * Free Software Foundation; either version 2 of the License, or (at your
                      8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
                      9:  *
                     10:  * This program is distributed in the hope that it will be useful, but
                     11:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
                     13:  * for more details.
                     14:  */
                     15: 
                     16: #include "load_tester_control.h"
                     17: 
                     18: #include <sys/socket.h>
                     19: #include <sys/un.h>
                     20: #include <unistd.h>
                     21: #include <stddef.h>
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
                     25: #include <errno.h>
                     26: 
                     27: /**
                     28:  * Connect to the daemon, return stream
                     29:  */
                     30: static FILE* make_connection()
                     31: {
                     32:        struct sockaddr_un addr;
                     33:        FILE *stream;
                     34:        int fd;
                     35: 
                     36:        addr.sun_family = AF_UNIX;
                     37:        strcpy(addr.sun_path, LOAD_TESTER_SOCKET);
                     38: 
                     39:        fd = socket(AF_UNIX, SOCK_STREAM, 0);
                     40:        if (fd < 0)
                     41:        {
                     42:                fprintf(stderr, "opening socket failed: %s\n", strerror(errno));
                     43:                return NULL;
                     44:        }
                     45:        if (connect(fd, (struct sockaddr *)&addr,
                     46:                        offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path)) < 0)
                     47:        {
                     48:                fprintf(stderr, "connecting to %s failed: %s\n",
                     49:                                LOAD_TESTER_SOCKET, strerror(errno));
                     50:                close(fd);
                     51:                return NULL;
                     52:        }
                     53:        stream = fdopen(fd, "r+");
                     54:        if (!stream)
                     55:        {
                     56:                close(fd);
                     57:                return NULL;
                     58:        }
                     59:        return stream;
                     60: }
                     61: 
                     62: /**
                     63:  * Initiate load-tests
                     64:  */
                     65: static int initiate(unsigned int count, unsigned int delay)
                     66: {
                     67:        FILE *stream;
                     68:        int c;
                     69: 
                     70:        stream = make_connection();
                     71:        if (!stream)
                     72:        {
                     73:                return 1;
                     74:        }
                     75: 
                     76:        fprintf(stream, "%u %u\n", count, delay);
                     77: 
                     78:        while (1)
                     79:        {
                     80:                fflush(stream);
                     81:                c = fgetc(stream);
                     82:                if (c == EOF)
                     83:                {
                     84:                        break;
                     85:                }
                     86:                if (fputc(c, stdout) == EOF)
                     87:                {
                     88:                        break;
                     89:                }
                     90:                fflush(stdout);
                     91:        }
                     92:        fclose(stream);
                     93:        return 0;
                     94: }
                     95: 
                     96: int main(int argc, char *argv[])
                     97: {
                     98:        if (argc >= 3 && strcmp(argv[1], "initiate") == 0)
                     99:        {
                    100:                return initiate(atoi(argv[2]), argc > 3 ? atoi(argv[3]) : 0);
                    101:        }
                    102:        fprintf(stderr, "Usage:\n");
                    103:        fprintf(stderr, "  %s initiate <count> [<delay in ms>]\n", argv[0]);
                    104:        return 1;
                    105: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>