#ifdef __unix__ #include "acccre-server.h" #include #include /*#ifdef INET*/ #include /*#endif*/ #include #include #include #include #include #include #include /*#ifdef UNIX*/ #include /*#endif*/ #include volatile sig_atomic_t sig_flag = 0; /* TODO: call write() once; check for its errors */ static void sig_handler(int signo) { char *sig_name = strsignal(signo); char *sig_msg = " signal is caught\n"; write(STDOUT_FILENO, sig_name, strlen(sig_name)); write(STDOUT_FILENO, sig_msg, strlen(sig_msg)); sig_flag = 1; } void *process(void *arg) { int client_fd = *((int *)arg); char *buf = malloc(sizeof(acccre_size_t)); /* TODO: make a protocol */ while (sig_flag == 0) { if (recv(client_fd, buf, sizeof(acccre_size_t), 0) > 0) { /* Now, parse the request... */ acccre_size_t msg_len = *buf; printf("size of message is %d\n", (acccre_size_t)msg_len); buf = realloc(buf, msg_len); recv(client_fd, buf, msg_len, 0); char *msg = buf; printf("%s\n", msg); /* ... char *response = (char *)malloc(BUFFER_SIZE * sizeof(char)); snprintf(response, BUFFER_SIZE, ""); size_t response_len;*/ /* ... */ } } close(client_fd); free(arg); free(buf); return 0; } int main (int argc, char *argv[]) { struct sigaction sa = { sig_handler, /* sa_handler */ 0, /* sa_mask */ 0 /* sa_flags */ }; for (int i = 0; sigs[i] != SIGXFSZ; i++) { if (sigaction(sigs[i], &sa, NULL) != 0) { perror("sigaction()"); exit(EXIT_FAILURE); } } int server_fd; if ((server_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { perror("socket()"); exit(EXIT_FAILURE); } struct sockaddr_un sun = { AF_UNIX, /* sun_family */ DEFAULT_PATH /* sun_path */ }; if (argc > 1) { size_t sun_path_length = strlen(argv[1]); if (sun_path_length >= MAX_SUN_PATH) { fprintf(stderr, "Size of path to the socket (%zu) is greater than, or equal to the maximum size (%zu).\n", sun_path_length, MAX_SUN_PATH); exit(EXIT_FAILURE); } else { strcpy(sun.sun_path, argv[1]); } } if (bind(server_fd, (struct sockaddr *)&sun, sizeof(sun)) != 0) { perror("bind()"); exit(EXIT_FAILURE); } if (listen(server_fd, 5) != 0) { perror("listen()"); exit(EXIT_FAILURE); } puts("Ready!"); while (sig_flag == 0) { struct sockaddr_un client_addr; socklen_t client_addr_len = sizeof(client_addr); int *client_fd = malloc(sizeof(int)); *client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_len); if (*client_fd == -1) { perror("accept()"); free(client_fd); continue; } pthread_t thread_id; /* TODO */ pthread_create(&thread_id, NULL, process, (void *)client_fd); pthread_detach(thread_id); } close(server_fd); unlink(sun.sun_path); return 0; } #endif