summaryrefslogtreecommitdiff
path: root/daemon.c
blob: 7b6ba09dade2cd015a9900e05f5bb853402b8938 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "daemon.h"

#include "timer.h"

#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <assert.h>

static struct sockaddr_un addr = (struct sockaddr_un){.sun_path = "/tmp/pomo.sock", .sun_family = AF_UNIX};

int socket_create() {
  errno = 0;  
  int fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (fd == -1) {
    printf("Socket creation failed! %s\n", strerror(errno));
    fflush(stdout);        
    exit(1);
  }
  return fd;  
}

void socket_set_server(int sockfd) {
  errno = 0;
  if (bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
    printf("Socket bind failed! %s\n", strerror(errno));
    fflush(stdout);        
    exit(1);
  }
  errno = 0;
  if (listen(sockfd, 1) == -1) {
    printf("Socket listen() failed! %s\n", strerror(errno));
    exit(1);    
  }    
}

void socket_set_client(int sockfd) {
  errno = 0;
  if(connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
    printf("Socket connect failed! %s\n", strerror(errno));
    fflush(stdout);        
    exit(1);
  }
}

void socket_bind_clean(int sig) {
  unlink("/tmp/pomo.sock");
  exit(0);
}

void daemon_init(Daemon* d) {
  assert(d != NULL);
  socket_set_server(d->sockfd);
  signal(SIGINT, socket_bind_clean);
  signal(SIGTERM, socket_bind_clean);
  atexit((void *)socket_bind_clean);
}

void* daemon_listener_loop(void *d) {
  assert(d != NULL);
  while(1) {
    int clientfd = accept(((Daemon *)d)->sockfd, NULL, NULL);
      
    if (clientfd == -1) {
      printf("Incoming connection failed.\n");
      continue;
    }

    char buffer[1024];
    read(clientfd, buffer, 1024 - 1);

    // TODO: Create a mechanism to fetch commands without writing everything again.
    // TODO: Make it so that a more efficient approach is used instead of a string.
    if(strncmp(buffer, "start", sizeof("start")) == 0) { 
      timer_start(((Daemon *)d)->timer);
    }

    if(strncmp(buffer, "toggle", sizeof("toggle")) == 0) {
      timer_toggle_state(((Daemon *)d)->timer);
    }

    if(strncmp(buffer, "reset", sizeof("reset")) == 0) {
      timer_reset(((Daemon *)d)->timer);
    }
    
    /* send(clientfd, "done", sizeof("done"), 0); */
    close(clientfd);
  }
  return NULL;
}

void daemon_run(Daemon* d) {
  assert(d != NULL);

  Timer t;
  timer_init(&t, 3000);
  d->timer = &t;
  pthread_t thread;
  if(pthread_create(&thread, NULL, daemon_listener_loop, d) != 0) {
    printf("Error: daemon_listener_loop thread cannot be created!");
    exit(1);
  }
  
  timer_loop(&t);

  void* retval;
  pthread_join(thread, &retval);
}