Files
unix/lab4/main.c
2025-11-02 13:37:19 +01:00

57 lines
1.3 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Простой многопроцессный HTTP сервер с использованием fork()
*
* Компиляция: make
*
* Запуск: ./webserver <путь_к_папке> [порт]
* Примеры:
* ./webserver ./test-site
* ./webserver ./test-site 8080
*
* Остановка: pkill webserver
*
* Просмотр логов:
* sudo journalctl -f -t MY-WEB-SERVER
* sudo journalctl -f -t MY-WEB-SERVER --since "now" -p info
*
* Просмотр процессов:
* pstree -p $(pgrep -o webserver)
* ps aux | grep webserver
*/
#include "server.h"
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <www_root> [port]\n", argv[0]);
fprintf(stderr, "Example: %s ./test-site 13131\n", argv[0]);
return 1;
}
/* Преобразуем путь в абсолютный */
char abs_path[PATH_MAX];
if (realpath(argv[1], abs_path) == NULL) {
perror("Invalid path");
return 1;
}
int port = 13131;
if (argc > 2) {
port = atoi(argv[2]);
if (port <= 0 || port > 65535) {
fprintf(stderr, "Invalid port: %s\n", argv[2]);
return 1;
}
}
printf("Starting web server on port %d, serving %s...\n", port, abs_path);
server(abs_path, port);
return 0;
}