/* This is very, very old code, back from when I still used libgoth. */ /* argusd/main.c * * Entry point for argusd. * * Copyright (C) 2004 * Chris Lance * Zorica Majdov * Andy Goth * * This code is available under the GNU General Public License; see COPYING. */ #include #include #include #include #include #include #include #include #include "lib/vector.h" #include "libargus/libargus.h" char* socket_path; /* Path to the server socket. */ vector_t* clients; /* List of connected clients. */ vector_t* ports; /* List of ports monitored by argusd. */ vector_t* sensors; /* List of sensors on all ports. */ vector_t* pollfds; /* List of fd's watched by poll(). */ /* Connected client. */ typedef struct client { /* File descriptor for the socket on which the client is connected. */ int fd; /* List of sensors the client is listening for. Every time argusd receives * new data, it sends data packets to all connected clients who are * listening for at least one sensor. It only sends values for the * requested sensors, and it provides them in the order specified. If all * of the requested sensors have the same value as from the previous * sample, no data is sent. The values in this array are indices into * the global sensor arrays. */ vector_t* sensors; } client_t; /* Serial port and the sensor device attached to it. */ typedef struct port { /* File descriptor for the serial port. */ int fd; /* Filename of the serial port the sensor device is connected to. */ char* filename; /* Index of the first sensor in the global sensors array. */ int sensor_base; } port_t; /* Digital or analog sensor. */ typedef struct sensor { /* Name of the sensor, as understood by the clients. */ char* name; /* Current value of the sensor. */ int value; } sensor_t; int main(int argc, char** argv) { int fd; struct sockaddr_un saddr; int poll_ret; struct pollfd ufd; argus_ctx* ctx; /* Load the configuration file. */ read_config(argc, argv); /* Create the server socket. */ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { /* Socket already exists or other error. */ if (argus_connect(&ctx, socket_path) == 0) { /* Server is already running. */ argus_disconnect(ctx); fprintf(stderr, "argusd already running on %s\n", socket_path); exit(EXIT_FAILURE); } else { /* Server's not running--- socket is probably stale. */ if (unlink(sockname) == -1) perror("unlink"); /* Again try create the socket. */ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { /* Ack, something else is wrong. */ perror("socket"); exit(EXIT_FAILURE); } } } /* Create the socket. */ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(EXIT_FAILURE); } /* Open it for listening. */ saddr.sun_family = AF_UNIX; strncpy(saddr.sun_path, sockname, 108); if (bind(fd, (struct sockaddr*)&saddr, sizeof(saddr)) == -1) { perror("bind"); exit(EXIT_FAILURE); } if (listen(fd, 100) == -1) { perror("listen"); exit(EXIT_FAILURE); } /* Process incoming connections. */ ufd.fd = fd; ufd.events = POLLIN | POLLERR | POLLHUP | POLLNVAL; while (1) { poll_ret = poll(&ufd, 1, -1); if (poll_ret < 0) { perror("poll"); exit(EXIT_FAILURE); } else if (poll_ret == 0) continue; if (ufd.revents & (POLLERR | POLLHUP | POLLNVAL)) { perror("poll"); exit(EXIT_FAILURE); } if (ufd.revents & POLLIN) { printf("Received connection.\n"); fd = accept(ufd.fd, NULL, NULL); write(fd, "hello\n", 6); write(1, buf, read(fd, buf, 256)); close(fd); } } /* Close the socket and remove the socket node. */ if (close(fd) == -1) perror("close"); if (unlink(sockname) == -1) perror("unlink"); } /* vim: set ts=4 sts=4 sw=4 tw=80 et: */ /* EOF */