| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- /* vccd/main.c
- *
- * This file is part of vccd.
- *
- * vccd is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * vccd is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with vccd. If not, see <https://www.gnu.org/licenses/>
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <vcc/vcc.h>
- #include <vcc/version.h>
- /* well, it's not a really daemon because we didn't do things about
- * sessions or pgrps, and didn't close stdout stderr stdin etc.
- *
- * but that only makes things more difficult and hard to understand
- * that's why we just fork() and exit() */
- #define vcc_log(msg, ...) fprintf(stderr, msg, ##__VA_ARGS__)
- /* this is the shortest name i think. it stores the fd of the server
- * socket */
- int fd;
- static int usage(void) {
- fprintf(stderr, "Usage: vccd (no argument)\n");
- return 1;
- }
- static int becomes_daemon(void) {
- int pid;
- if ((pid = fork()))
- /* then prompt was there again */
- exit(0);
- umask(0);
- chdir("/");
- close(0);
- if (unlikely(open("/var/log/vccd.log", O_WRONLY | O_CREAT, 0644) < 0)) {
- perror("open(/var/run/vccd.log)");
- exit(1);
- }
- close(1);
- close(2);
- (void) dup(0);
- (void) dup(0);
- if (unlikely(fd = open("/var/run/vccd.pid", O_WRONLY | O_CREAT, 0644)) < 0) {
- perror("open(/var/run/vccd.pid)");
- exit(1);
- }
- dprintf(fd, "%d\n", getpid());
- close(fd);
- /* fd 3 is now /var/run/vccd.pid */
- vcc_log(VCCD_VERSION " started. \n");
- return 0;
- }
- static int init_socket(void) {
- struct sockaddr_in addr;
- if (unlikely((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) {
- perror("socket()");
- exit(1);
- }
- memset(&addr, 0, sizeof(struct sockaddr_in));
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
- addr.sin_port = htons(VCC_PORT);
- if (unlikely(bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)) {
- perror("bind()");
- exit(1);
- }
- if (unlikely(listen(fd, SOMAXCONN) < 0)) {
- perror("listen()");
- exit(1);
- }
- return fd;
- }
- int main(int argc, const char **argv) {
- __uint32_t size;
- int usr_fd;
- char *buf;
- struct sockaddr_in usr_addr;
- if (unlikely(argc != 1))
- return usage();
- becomes_daemon();
- init_socket();
- for (;;) {
- size = sizeof(struct sockaddr_in);
- if (unlikely(usr_fd = accept(fd, (struct sockaddr *) &usr_addr, &size)) < 0) {
- perror("accept()");
- continue;
- }
- vcc_log("New connection from %s\n", inet_ntoa(usr_addr.sin_addr));
-
- if (unlikely(!(buf = malloc(REQ_SIZE)))) {
- fprintf(stderr, "malloc() failed\n");
- continue;
- }
- size = read(usr_fd, buf, REQ_SIZE);
- vcc_log("package size %u: %s\n", size, buf);
- }
- return 0;
- }
|