still not making any progress

This commit is contained in:
2025-07-21 22:39:42 +02:00
parent c776501adc
commit 4d8fd7b98e

View File

@ -6,7 +6,7 @@
#include <sys/socket.h>
#include <sys/un.h>
pid_t pid;
pid_t pid, sid;
int server_sock;
struct sockaddr_un server;
int sockaddr_len;
@ -16,13 +16,11 @@ int main() {
Setup daemon
Used this for basic idea => https://en.wikipedia.org/wiki/Daemon_(computing)
*/
pid_t sid;
pid = fork();
if (pid == -1) {
fprintf(stderr, "Couldn't fork\n");
return 1;
} else if (pid > 0) {
} else if (pid != 0) {
printf("Pid of child is %d\n", pid);
return 0;
}
@ -40,19 +38,20 @@ int main() {
close(STDOUT_FILENO);
close(STDERR_FILENO);
FILE *stdout;
FILE *stderr;
FILE *out_log;
FILE *err_log;
/* TODO: switch to mode 'a' later */
stdout = fopen("/tmp/out_log.txt", "w");
stderr = fopen("/tmp/err_log.txt", "w");
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 (sock == -1) {
if (server_sock == -1) {
return 1;
}
@ -69,39 +68,42 @@ int main() {
return 1;
}
printf("Listening on %s\n", server.sun_path);
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(sock, NULL, NULL);
if (sock2 == -1) {
client_sock = accept(server_sock, NULL, NULL);
if (client_sock == -1) {
return 1;
}
data_length = recv(sock2, receive_buf, sizeof receive_buf, 0);
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, strops_length(send_buf), 0);
send(client_sock, send_buf, strlen(send_buf), 0);
close(client_sock);
continue;
}
if (strops_contains(send_buf, "shutdown now!")) {
if (strstr(send_buf, "shutdown") != NULL) {
strcpy(send_buf, "Shutting down.");
send(client_sock, send_buf, strops_length(send_buf), 0);
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;
}