Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 93 lines (74 sloc) 2.083 kB
b13ae77 @kr better flag parsing (with tests)
authored
1 #include <stdint.h>
50b5c5e Initial commit of Beanstalk.
Keith Rarick authored
2 #include <signal.h>
3b3b672 Add a -d (detach) command line option (#931).
Keith Rarick authored
3 #include <stdio.h>
50b5c5e Initial commit of Beanstalk.
Keith Rarick authored
4 #include <stdlib.h>
3f8b9b8 Option to set the port (#931).
Keith Rarick authored
5 #include <errno.h>
be1e525 Make beanstalk be a good little daemon.
Keith Rarick authored
6 #include <sys/stat.h>
6e9c54a Implement "-u" switch user option.
Keith Rarick authored
7 #include <unistd.h>
8 #include <pwd.h>
6b83cb6 @kr clean up the build
authored
9 #include <fcntl.h>
9947f12 @kr use more readable type names
authored
10 #include "dat.h"
42ee606 Collect most stats. This is for bug #595.
Keith Rarick authored
11
50b5c5e Initial commit of Beanstalk.
Keith Rarick authored
12 static void
6e9c54a Implement "-u" switch user option.
Keith Rarick authored
13 su(const char *user) {
14 int r;
15 struct passwd *pwent;
16
17 errno = 0;
18 pwent = getpwnam(user);
19 if (errno) twarn("getpwnam(\"%s\")", user), exit(32);
20 if (!pwent) twarnx("getpwnam(\"%s\"): no such user", user), exit(33);
21
22 r = setgid(pwent->pw_gid);
23 if (r == -1) twarn("setgid(%d \"%s\")", pwent->pw_gid, user), exit(34);
24
25 r = setuid(pwent->pw_uid);
26 if (r == -1) twarn("setuid(%d \"%s\")", pwent->pw_uid, user), exit(34);
27 }
28
8a612c9 Make sure qprof works on interrupt.
Keith Rarick authored
29
50b5c5e Initial commit of Beanstalk.
Keith Rarick authored
30 static void
31 set_sig_handlers()
32 {
33 int r;
34 struct sigaction sa;
35
36 sa.sa_handler = SIG_IGN;
37 sa.sa_flags = 0;
38 r = sigemptyset(&sa.sa_mask);
6f84c28 More informative error messages.
Keith Rarick authored
39 if (r == -1) twarn("sigemptyset()"), exit(111);
50b5c5e Initial commit of Beanstalk.
Keith Rarick authored
40
41 r = sigaction(SIGPIPE, &sa, 0);
6f84c28 More informative error messages.
Keith Rarick authored
42 if (r == -1) twarn("sigaction(SIGPIPE)"), exit(111);
977740b Make a "drain mode" to ease upgrading.
Keith Rarick authored
43
44 sa.sa_handler = enter_drain_mode;
45 r = sigaction(SIGUSR1, &sa, 0);
6f84c28 More informative error messages.
Keith Rarick authored
46 if (r == -1) twarn("sigaction(SIGUSR1)"), exit(111);
50b5c5e Initial commit of Beanstalk.
Keith Rarick authored
47 }
48
49 int
50 main(int argc, char **argv)
51 {
a27c08e @kr schedule ticks explicitly
authored
52 int r;
8c0633c @kr redo the WAL code, improving style
authored
53 struct job list = {};
50b5c5e Initial commit of Beanstalk.
Keith Rarick authored
54
2e1e9e2 Supply warn() and warnx(); don't use the library.
Keith Rarick authored
55 progname = argv[0];
7abe44a @kr line-buffered stdout, even when not on a tty
authored
56 setlinebuf(stdout);
d15e726 @kr improve the tests
authored
57 optparse(&srv, argv+1);
3b3b672 Add a -d (detach) command line option (#931).
Keith Rarick authored
58
d8cadfc @kr verbose mode; closes #52
authored
59 if (verbose) {
60 printf("pid %d\n", getpid());
61 }
62
d15e726 @kr improve the tests
authored
63 r = make_server_socket(srv.addr, srv.port);
1ab5fde @kr add integration test in C
authored
64 if (r == -1) twarnx("make_server_socket()"), exit(111);
d15e726 @kr improve the tests
authored
65 srv.sock.fd = r;
1ab5fde @kr add integration test in C
authored
66
86c1e4a Refactoring.
Keith Rarick authored
67 prot_init();
60b7903 Bug #882 - put uptime in beanstalk stats.
Keith Rarick authored
68
d15e726 @kr improve the tests
authored
69 if (srv.user) su(srv.user);
50b5c5e Initial commit of Beanstalk.
Keith Rarick authored
70 set_sig_handlers();
71
d15e726 @kr improve the tests
authored
72 if (srv.wal.use) {
8c0633c @kr redo the WAL code, improving style
authored
73 // We want to make sure that only one beanstalkd tries
74 // to use the wal directory at a time. So acquire a lock
75 // now and never release it.
d15e726 @kr improve the tests
authored
76 if (!waldirlock(&srv.wal)) {
77 twarnx("failed to lock wal dir %s", srv.wal.dir);
8c0633c @kr redo the WAL code, improving style
authored
78 exit(10);
79 }
80
81 list.prev = list.next = &list;
d15e726 @kr improve the tests
authored
82 walinit(&srv.wal, &list);
2f99e8c @kr check return value
authored
83 r = prot_replay(&srv, &list);
84 if (!r) {
85 twarnx("failed to replay log");
86 return 1;
87 }
1ab5fde @kr add integration test in C
authored
88 }
50b5c5e Initial commit of Beanstalk.
Keith Rarick authored
89
d15e726 @kr improve the tests
authored
90 srvserve(&srv);
50b5c5e Initial commit of Beanstalk.
Keith Rarick authored
91 return 0;
92 }
Something went wrong with that request. Please try again.