still not making any progress
This commit is contained in:
38
src/hdbd.c
38
src/hdbd.c
@ -6,7 +6,7 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
|
|
||||||
pid_t pid;
|
pid_t pid, sid;
|
||||||
int server_sock;
|
int server_sock;
|
||||||
struct sockaddr_un server;
|
struct sockaddr_un server;
|
||||||
int sockaddr_len;
|
int sockaddr_len;
|
||||||
@ -16,13 +16,11 @@ int main() {
|
|||||||
Setup daemon
|
Setup daemon
|
||||||
Used this for basic idea => https://en.wikipedia.org/wiki/Daemon_(computing)
|
Used this for basic idea => https://en.wikipedia.org/wiki/Daemon_(computing)
|
||||||
*/
|
*/
|
||||||
pid_t sid;
|
|
||||||
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid == -1) {
|
if (pid == -1) {
|
||||||
fprintf(stderr, "Couldn't fork\n");
|
fprintf(stderr, "Couldn't fork\n");
|
||||||
return 1;
|
return 1;
|
||||||
} else if (pid > 0) {
|
} else if (pid != 0) {
|
||||||
printf("Pid of child is %d\n", pid);
|
printf("Pid of child is %d\n", pid);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -40,19 +38,20 @@ int main() {
|
|||||||
close(STDOUT_FILENO);
|
close(STDOUT_FILENO);
|
||||||
close(STDERR_FILENO);
|
close(STDERR_FILENO);
|
||||||
|
|
||||||
FILE *stdout;
|
FILE *out_log;
|
||||||
FILE *stderr;
|
FILE *err_log;
|
||||||
/* TODO: switch to mode 'a' later */
|
/* TODO: switch to mode 'a' later */
|
||||||
stdout = fopen("/tmp/out_log.txt", "w");
|
out_log = fopen("/tmp/out_log.txt", "w");
|
||||||
stderr = fopen("/tmp/err_log.txt", "w");
|
err_log = fopen("/tmp/err_log.txt", "w");
|
||||||
if (out_log == NULL || err_log == NULL) {
|
if (out_log == NULL || err_log == NULL) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fprintf(err_log, "Hello, World!\n");
|
||||||
|
|
||||||
/* Setup Unix Socket */
|
/* Setup Unix Socket */
|
||||||
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
if (sock == -1) {
|
if (server_sock == -1) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,39 +68,42 @@ int main() {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Listening on %s\n", server.sun_path);
|
fprintf(out_log, "Listening on %s\n", server.sun_path);
|
||||||
|
|
||||||
|
|
||||||
/* Actual program */
|
/* Actual program */
|
||||||
int client_sock;
|
int client_sock;
|
||||||
|
int data_length = 0;
|
||||||
char receive_buf[1000];
|
char receive_buf[1000];
|
||||||
char send_buf[1000];
|
char send_buf[1000];
|
||||||
memset(receive_buf, 0, sizeof receive_buf);
|
memset(receive_buf, 0, sizeof receive_buf);
|
||||||
memset(send_buf, 0, sizeof send_buf);
|
memset(send_buf, 0, sizeof send_buf);
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
client_sock = accept(sock, NULL, NULL);
|
client_sock = accept(server_sock, NULL, NULL);
|
||||||
if (sock2 == -1) {
|
if (client_sock == -1) {
|
||||||
return 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) {
|
if (data_length <= 0) {
|
||||||
strcpy(send_buf, "Failed to receive any data.");
|
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);
|
close(client_sock);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strops_contains(send_buf, "shutdown now!")) {
|
if (strstr(send_buf, "shutdown") != NULL) {
|
||||||
strcpy(send_buf, "Shutting down.");
|
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);
|
close(client_sock);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
close(client_sock);
|
close(client_sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fclose(out_log);
|
||||||
|
fclose(err_log);
|
||||||
close(server_sock);
|
close(server_sock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user