summaryrefslogtreecommitdiff
path: root/pullreqd.c
blob: d4e67eac9915646406349283dde1d85bbdd8517d (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <signal.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <syslog.h>
#include <unistd.h>
#include "config.h"
#include "git.h"
#include "linked_list.h"
#include "macros.h"
#include "network.h"

#define UNIX_PATH_MAX 108

pid_t fork_proc() {
	/* Fork off the parent. */
	pid_t pid = fork();

	/* The process id is non zero, so it must be the parent process. */
	if (pid) {
		/* Exit with EXIT_FAILURE if the parent's pid is
		 * negative, otherwise exit with EXIT_SUCCESS.
		 */
		exit((pid < 0) ? EXIT_FAILURE : EXIT_SUCCESS);
	}
	return pid;
}

void child_handler(int sig_num) {
	signal(SIGCHLD, child_handler);
}

void hangup_handler(int sig_num) {
	signal(SIGHUP, hangup_handler);
}

void init_daemon(int change_dir, char *path) {
	/* Fork the parent. */
	pid_t pid = fork_proc();

	/* Exit if the child process isn't the session leader. */
	if (setsid() < 0) {
		exit(EXIT_FAILURE);
	}

	/* Catch, ignore, and/or handle signals. */
	signal(SIGCHLD, child_handler);
	signal(SIGHUP, hangup_handler);

	/* Fork again. */
	pid = fork_proc();

	/* Set new file permissions. */
	umask(0);

	/* If change_dir is true, change the working directory to path, or
	 * the root directory if path isn't set.
	 */
	if (change_dir) {
		chdir((!path) ? "/" : path);
	}

	/* Close any open file descriptors. */
	for (int i = sysconf(_SC_OPEN_MAX); i >= 0; close(i--));
}

int init_config(char *config_file, config **cfg) {
	log(LOG_INFO, "Reading config file %s.", config_file);

	*cfg = parse_config(config_file);

	/* Did the config file parser succeed? */
	if (*cfg != NULL) {
		log(LOG_INFO, "Successfully read %s.", config_file);
	} else {
		log(LOG_ERR, "Error reading %s.", config_file);
	}

	return (*cfg != NULL);
}

int *init_socket(config *cfg) {
	linked_list *sock_list = NULL;
	int *sock_arr = NULL;
	/* Set socket type to unix socket, if socket-type was set to unix, otherwise set it to network socket. */
	const int sock_type = (strcasecmp(cfg->sock_type, "unix") == 0) ? AF_UNIX : AF_INET;

	if (sock_type == AF_UNIX) {
		size_t path_length = strlen(cfg->sock);
		/* Is the path too long? */
		if (path_length >= UNIX_PATH_MAX) {
			log(LOG_ERR, "The socket path is too long, and exceeded %i characters.", UNIX_PATH_MAX);
			return NULL;
		} else {
			int fd;
			struct sockaddr_un sa_un;
			sa_un.sun_family = sock_type;
			memcpy(sa_un.sun_path, cfg->sock, path_length+1);

			fd = create_socket((struct sockaddr *)&sa_un, sizeof(struct sockaddr_un));

			if (fd < 0) {
				return NULL;
			} else {
				int *sock = calloc(1, sizeof(int));
				*sock = fd;
				sock_list = add_node(&sock_list, sock);
			}
		}
	} else {
		struct addrinfo hints, *ainfo_root, *ainfo;

		memset(&hints, 0, sizeof(hints));

		hints.ai_family = AF_UNSPEC;
		hints.ai_socktype = SOCK_SEQPACKET;
		hints.ai_protocol = IPPROTO_SCTP;
		hints.ai_flags = AI_PASSIVE;

		const int gai_ret = getaddrinfo(cfg->sock, cfg->port, &hints, &ainfo_root);
		/* Did getaddrinfo fail? */
		if (gai_ret) {
			log_reason(LOG_ERR, "getaddrinfo() failed for hostname %s.", cfg->sock, gai_strerror(gai_ret));
			return NULL;
		}

		for (struct addrinfo *ai = ainfo_root; ai != NULL; ai = ai->ai_next) {
			const int fd = create_socket(ai->ai_addr, ai->ai_addrlen);
			if (fd >= 0) {
				int *sock = calloc(1, sizeof(int));
				*sock = fd;
				sock_list = add_node(&sock_list, sock);
				log(LOG_DEBUG, "ip addr: \"%s\"", addr_to_str(ai->ai_addr, ai->ai_addrlen));
			}
		}

		freeaddrinfo(ainfo_root);
	}

	int sock_count = linked_list_size(sock_list);
	sock_arr = calloc(sock_count+1, sizeof(int));
	sock_arr[sock_count--] = -1;

	for (linked_list *node = sock_list; node != NULL; node = node->prev, --sock_count) {
		int *sock = (int *)node->data;
		sock_arr[sock_count] = *sock;
		node->data = NULL;
		free(sock);
	}

	log(LOG_NOTICE, "Successfully created %i sockets for hostname \"%s\".", linked_list_size(sock_list), (cfg->sock != NULL) ? cfg->sock : "*");

	cleanup_linked_list(sock_list);

	return sock_arr;
}

int main_loop(config *cfg, int *listen_sockets, git_repo **repos) {
	int done = 0;
	struct stat st;

	index_t *idx = &(index_t) {
		.type = 1,
		.num = 0,
		.next = NULL,
	};
	index_t *idx2 = &(index_t) {
		.type = 1,
		.num = 10,
		.next = NULL,
	};
	file **patches = (file *[]) {
		&(file){"lol.patch", "asfklhasflhk"},
		&(file){"oof.patch", "you just got rekt by some young guy"},
		NULL
	};
	pull_request *pr = &(pull_request){"lol", "oof", "some young guy", NULL, time(NULL), 0, {.patches = patches}, NULL};
	pull_request *pr2 = &(pull_request){"Give actkbd, the ability to turn into an epic Unix utility", "actkbd seems like a cool concept for a keyboard shortcut daemon, but I realised that it was very easy to print keypresses to stdout.\nSo that was what I did, but that was not enough, I really wanted the ability to get the keystate, every tick, which I did.\nOh yeah, I also fixed the regex used for autodetecting a keyboard, so that it actually works.\nHope you guys enjoy it!", "mrb0nk500", NULL, 1568423431, 1, {.branch = &(git_branch){.name = "master", .repo = "https://github.com/mrb0nk500/actkbd.git"}}, "master"};

	/* Does the PR root directory exist? */
	if (stat(cfg->pr_root, &st) < 0) {
		/* Create the PR root directory. */
		mkdir(cfg->pr_root, 0755);
	}

	for (; !done;) {
		pull_request *new_pr;
		if (create_pull_request_dir(pr, idx, cfg->pr_root, "") < 0) {
			log(LOG_ERR, "Failed to create Pull Request directory.");
			return -1;
		}
		new_pr = get_pull_request(idx, cfg->pr_root, "");
		if (new_pr == NULL) {
			char *idx_str = index_to_str(idx);
			log(LOG_ERR, "Failed to get Pull Request #%s.", idx_str);
			free(idx_str);
		} else {
			cleanup_pull_request(new_pr);
			new_pr = NULL;
		}

		if (create_pull_request_dir(pr2, idx2, cfg->pr_root, "actkbd") < 0) {
			log(LOG_ERR, "Failed to create second Pull Request directory.");
			return -1;
		}

		if (create_pull_request_branch(pr2, idx2, get_repo(repos, "actkbd")) < 0) {
			log(LOG_ERR, "Failed to create Pull Request branch.");
			return -1;
		}
		break;
	}

	return 0;
}

void cleanup(config *cfg, int *listen_sockets, git_repo **repos) {
	if (listen_sockets != NULL) {
		for (int i = 0; listen_sockets[i] >= 0; ++i) {
			struct sockaddr *sa = get_sock_addr(listen_sockets[i]);
			/* Close the socket. */
			close(listen_sockets[i]);
			/* Is this a unix domain socket? */
			if (sa->sa_family == AF_UNIX) {
				struct sockaddr_un *sa_un = (struct sockaddr_un *)sa;
				/* Unlink the path. */
				unlink(sa_un->sun_path);
			}
			free(sa);
		}
		free(listen_sockets);
	}

	if (cfg) {
		cleanup_config(cfg);
	}

	if (repos) {
		cleanup_git(repos);
	}
}

int main(int argc, char **argv) {
	config *cfg = NULL;
	git_repo **repos = NULL;
	char *config_file = "test.conf";
	int config_read = 0;
	int *listen_sockets = NULL;
	int exit_status = EXIT_FAILURE;

	/* Start it in daemon mode. */
	/*init_daemon(1, "/");*/

	/* Open the logfile. */
	openlog("pullreqd", LOG_PID, LOG_DAEMON);

	log(LOG_NOTICE, "pullreqd started.");

	/* Read the config file. */
	config_read = init_config(config_file, &cfg);

	/* Did we successfully read the config file? */
	if (config_read) {
		/* Create the Listen socket(s). */
		listen_sockets = init_socket(cfg);
	}

	/* Did we successfully read the config file, and create any listen sockets? */
	if (config_read && listen_sockets != NULL) {
		repos = init_git(cfg);
		if (repos != NULL) {
			exit_status = main_loop(cfg, listen_sockets, repos);
		}
	}

	cleanup(cfg, listen_sockets, repos);

	log(LOG_NOTICE, "pullreqd stopped.");

	closelog();

	return exit_status;
}