Compare commits

5 Commits

Author SHA1 Message Date
4d8fd7b98e still not making any progress 2025-07-21 22:39:42 +02:00
c776501adc new unix socket stuff
I can't get logfiles to work though
2025-07-19 23:47:34 +02:00
e0274fb6d5 add some TODOs 2025-06-30 22:30:06 +02:00
9d6fa83a43 sort generated file 2025-06-05 22:27:11 +02:00
31dc467165 setup daemon 2025-06-01 22:40:56 +02:00
3 changed files with 113 additions and 1 deletions

View File

@@ -6,6 +6,9 @@ hdb:
gcc -ansi -O2 -pipe -o build/hdb src/main.c src/db.c -lsqlite3
strip build/hdb
hdbd: src/db.c src/hdbd.c
gcc -ansi -o build/hdbd src/hdbd.c src/db.c -lsqlite3 -lstrops
.PHONY: install uninstall
install: hdb

View File

@@ -213,7 +213,7 @@ int h_db_generate_file(FILE *fptr, const char *website_name) {
int result;
char *error_msg;
sprintf(sql, "SELECT * FROM hentai WHERE website_name = '%s';", website_name);
sprintf(sql, "SELECT * FROM hentai WHERE website_name = '%s' ORDER BY url ASC;", website_name);
result = sqlite3_exec(db, sql, fprintf_callback, fptr, &error_msg);
free(sql);
if (result != SQLITE_OK) {

109
src/hdbd.c Normal file
View File

@@ -0,0 +1,109 @@
#include <strops.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
pid_t pid, sid;
int server_sock;
struct sockaddr_un server;
int sockaddr_len;
int main() {
/*
Setup daemon
Used this for basic idea => https://en.wikipedia.org/wiki/Daemon_(computing)
*/
pid = fork();
if (pid == -1) {
fprintf(stderr, "Couldn't fork\n");
return 1;
} else if (pid != 0) {
printf("Pid of child is %d\n", pid);
return 0;
}
sid = setsid();
if (sid == -1) {
return 1;
}
if (chdir("/") == -1) {
return 1;
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
FILE *out_log;
FILE *err_log;
/* TODO: switch to mode 'a' later */
out_log = fopen("/tmp/out_log.txt", "w");
err_log = fopen("/tmp/err_log.txt", "w");
if (out_log == NULL || err_log == NULL) {
return 1;
}
fprintf(err_log, "Hello, World!\n");
/* Setup Unix Socket */
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (server_sock == -1) {
return 1;
}
server.sun_family = AF_UNIX;
strcpy(server.sun_path, "/tmp/test_sock");
unlink(server.sun_path);
sockaddr_len = strlen(server.sun_path) + sizeof(server.sun_family);
if (bind(server_sock, (struct sockaddr*)&server, sockaddr_len) == -1) {
return 1;
}
if (listen(server_sock, 3) == -1) {
return 1;
}
fprintf(out_log, "Listening on %s\n", server.sun_path);
/* Actual program */
int client_sock;
int data_length = 0;
char receive_buf[1000];
char send_buf[1000];
memset(receive_buf, 0, sizeof receive_buf);
memset(send_buf, 0, sizeof send_buf);
while(1) {
client_sock = accept(server_sock, NULL, NULL);
if (client_sock == -1) {
return 1;
}
data_length = recv(client_sock, receive_buf, sizeof receive_buf, 0);
if (data_length <= 0) {
strcpy(send_buf, "Failed to receive any data.");
send(client_sock, send_buf, strlen(send_buf), 0);
close(client_sock);
continue;
}
if (strstr(send_buf, "shutdown") != NULL) {
strcpy(send_buf, "Shutting down.");
send(client_sock, send_buf, strlen(send_buf), 0);
close(client_sock);
break;
}
close(client_sock);
}
fclose(out_log);
fclose(err_log);
close(server_sock);
return 0;
}