File: bootstrap.c

package info (click to toggle)
workflow 0.11.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,744 kB
  • sloc: cpp: 33,792; ansic: 9,393; makefile: 9; sh: 6
file content (130 lines) | stat: -rw-r--r-- 2,481 bytes parent folder | download
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netdb.h>
#include <signal.h>

int flag = 0;

void sig_handler(int signo)
{
	if (signo == SIGUSR1)
		flag = 1;
	else if (signo == SIGINT || signo == SIGTERM)
		flag = 2;
}

int main(int argc, const char *argv[])
{
	if (argc != 3)
	{
		fprintf(stderr, "USAGE: %s EXEC_PROCESS PORT\n"
				"Bootstrap for workflow server to restart gracefully.\n",
				argv[0]);
		exit(1);
	}

	unsigned short port = atoi(argv[2]);
	int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
	struct sockaddr_in sin;

	memset(&sin, 0, sizeof sin);
	sin.sin_family = AF_INET;
	sin.sin_port = htons(port);
	sin.sin_addr.s_addr = htonl(INADDR_ANY);

	if (bind(listen_fd, (struct sockaddr *)&sin, sizeof sin) < 0)
	{
		close(listen_fd);
		perror("bind error");
		exit(1);
	}

	pid_t pid;
	int pipe_fd[2];
	ssize_t len;
	char buf[100];
	int status;
	int ret;

	char listen_fd_str[10] = { 0 };
	char write_fd_str[10] = { 0 };
	sprintf(listen_fd_str, "%d", listen_fd);

	signal(SIGINT, sig_handler);
	signal(SIGTERM, sig_handler);
	signal(SIGUSR1, sig_handler);

	while (flag < 2)
	{
		if (pipe(pipe_fd) == -1)
		{
			perror("open pipe error");
			exit(1);
		}

		memset(write_fd_str, 0, sizeof write_fd_str);
		sprintf(write_fd_str, "%d", pipe_fd[1]);

		pid = fork();
		if (pid < 0)
		{
			perror("fork error");
			close(pipe_fd[0]);
			close(pipe_fd[1]);
			break;
		}
		else if (pid == 0)
		{
			close(pipe_fd[0]);
			execlp(argv[1], argv[1], listen_fd_str, write_fd_str, NULL);
		}
		else
		{
			close(pipe_fd[1]);

			status = 0;
			ret = 0;
			flag = 0;
			fprintf(stderr, "Bootstrap daemon running with server pid-%d. "
					"Send SIGUSR1 to RESTART or SIGTERM to STOP.\n", pid);

			while (1)
			{
				ret = waitpid(pid, &status, WNOHANG);
				if (ret == -1 || !WIFEXITED(status) || flag != 0)
					break;

				sleep(3);
			}

			if (ret != -1 && WIFEXITED(status))
			{
				signal(SIGCHLD, SIG_IGN);

				kill(pid, SIGUSR1);
				fprintf(stderr, "Bootstrap daemon SIGUSR1 to pid-%ld %sing.\n",
						(long)pid, flag == 1 ? "restart" : "stop");

				len = read(pipe_fd[0], buf, 7);
				fprintf(stderr, "Bootstrap server served %*s.\n", (int)len, buf);
			}
			else
			{
				fprintf(stderr, "child exit. status = %d, waitpid ret = %d\n",
						WEXITSTATUS(status), ret);
				flag = 2;
			}

			close(pipe_fd[0]);
		}
	}

	close(listen_fd);
	return 0;
}