[canopy-httpd] r1100 committed - combine all of the global server flags into a single bit field `httpd_...

0 views
Skip to first unread message

codesite...@google.com

unread,
Nov 29, 2010, 11:19:39 PM11/29/10
to canopy-s...@googlegroups.com
Revision: 1100
Author: phrakt
Date: Mon Nov 29 20:18:37 2010
Log: combine all of the global server flags into a single bit field
`httpd_flags'

http://code.google.com/p/canopy-httpd/source/detail?r=1100

Modified:
/trunk/src/sbin/httpd/conf.c
/trunk/src/sbin/httpd/ctl.c
/trunk/src/sbin/httpd/httpd.c
/trunk/src/sbin/httpd/httpd.h
/trunk/src/sbin/httpd/log.c
/trunk/src/sbin/httpd/private.h

=======================================
--- /trunk/src/sbin/httpd/conf.c Tue Apr 20 20:51:21 2010
+++ /trunk/src/sbin/httpd/conf.c Mon Nov 29 20:18:37 2010
@@ -256,7 +256,6 @@

sock = http_sock_bind((struct sockaddr *)&sa_in, sizeof(sa_in));
if (sock == NULL) {
- httpd_log_err("listen failed");
return (-1);
}

=======================================
--- /trunk/src/sbin/httpd/ctl.c Tue Apr 20 20:51:21 2010
+++ /trunk/src/sbin/httpd/ctl.c Mon Nov 29 20:18:37 2010
@@ -163,6 +163,7 @@
}

httpd_ctl_sock = fd;
+ httpd_flags |= HTTPD_FLAG_CTLSOCK_CREATED;

return (0);
}
@@ -174,7 +175,6 @@
void
httpd_ctl_close(void)
{
-
if (httpd_ctl_conn != -1) {
httpd_log_info("closing control connection");
(void)close(httpd_ctl_conn);
@@ -187,7 +187,8 @@
httpd_ctl_sock = -1;
}

- if (unlink(httpd_ctl_sockpath) == -1)
+ if ((httpd_flags & HTTPD_FLAG_CTLSOCK_CREATED) &&
+ (unlink(httpd_ctl_sockpath) == -1))
httpd_log_errno("failed to unlink control socket "
"file %s", httpd_ctl_sockpath);
}
=======================================
--- /trunk/src/sbin/httpd/httpd.c Tue Apr 20 20:51:21 2010
+++ /trunk/src/sbin/httpd/httpd.c Mon Nov 29 20:18:37 2010
@@ -58,9 +58,6 @@
/* from libcanopy */
extern cnp_log_t *cnp_logchan;

-/* from log.c */
-extern int httpd_log_usesyslog;
-

/*
* Version information
@@ -78,21 +75,13 @@
*
*/
/* state of the server and shutdown/restart request flags */
-volatile sig_atomic_t httpd_state = HTTPD_UNKNOWN_STATE;
+volatile sig_atomic_t httpd_state = HTTPD_UNKNOWN_STATE;
+volatile sig_atomic_t httpd_flags = HTTPD_FLAG_DAEMON | HTTPD_FLAG_PIDFILE;
+
static volatile sig_atomic_t httpd_shutdown_flag = 0;
static volatile sig_atomic_t httpd_restart_flag = 0;


-/*
- * Daemon flag
- * -----------
- * This flag is used to tell whether we should start as a daemon. If the
flag
- * is set to 1, the server will fork in the background and become a daemon,
- * and increase the value of the flag to 2, to indicate that it is now a
- * daemon.
- */
-int httpd_daemon = 1;
-

pid_t httpd_pid;
char httpd_pidfile_path[_POSIX_PATH_MAX] = "/var/run/canopy.pid";
@@ -138,24 +127,25 @@
int httpd_mib_populate (void);


-static void usage (void);
-static int httpd_debug (void);
-static int httpd_trace (void);
-static int httpd_daemonize (void);
-static int httpd_writepid (void);
-static void httpd_loop (void);
-static int httpd_chroot (void);
+static void httpd_usage (void);
+static int httpd_debug (void);
+static int httpd_trace (void);
+static int httpd_daemonize (void);
+static int httpd_pidfile_create (void);
+static int httpd_pidfile_unlink (void);
+static void httpd_loop (void);
+static int httpd_chroot (void);


/*
- * usage()
+ * httpd_usage()
*
* Print usage information for the server to standard error.
*/
static void
-usage(void)
+httpd_usage(void)
{
fprintf(stderr,
"Usage: %s [-dfhStVv] [-c config] [-g group] [-P path] [-r dir] "
@@ -187,6 +177,9 @@
struct group *grp;

httpd_state = HTTPD_INITIALIZING;
+ httpd_flags = HTTPD_FLAG_DAEMON | HTTPD_FLAG_PIDFILE |
+ HTTPD_FLAG_CTLSOCK;
+
httpd_pid = getpid();

/* before anything else, give libhttp a chance to initialize itself */
@@ -196,6 +189,7 @@
CNP_LOG_CLRMASK(mask);
CNP_LOG_MASKPRI(mask, CNP_LOG_TRACE);
CNP_LOG_MASKPRI(mask, CNP_LOG_DEBUG);
+ CNP_LOG_MASKPRI(mask, CNP_LOG_INFO);

if ((httpd_logchan = cnp_log_open("httpd", 0, mask)) == NULL)
errx(1, "failed to allocate log channel");
@@ -257,7 +251,7 @@
httpd_debug();
break;
case 'f':
- httpd_daemon = FALSE;
+ httpd_flags &= ~HTTPD_FLAG_DAEMON;
break;
case 'g':
if ((grp = getgrnam(optarg)) != NULL) {
@@ -272,7 +266,7 @@

break;
case 'h':
- usage();
+ httpd_usage();
exit(0);

break; /* NOTREACHED */
@@ -283,6 +277,7 @@
errx(1, "PID file path too long");
break;
case 'r':
+ httpd_flags |= HTTPD_FLAG_CHROOT;
httpd_chroot_flag = 1;
len = strlcpy(httpd_chroot_path, optarg,
sizeof(httpd_chroot_path));
@@ -290,7 +285,7 @@
errx(1, "root directory path too long");
break;
case 'S':
- httpd_log_usesyslog = 1;
+ httpd_flags |= HTTPD_FLAG_SYSLOG;
break;
case 't':
httpd_trace();
@@ -315,7 +310,7 @@
case 'v':
break;
default:
- usage();
+ httpd_usage();
exit(1);

break; /* NOTREACHED */
@@ -332,18 +327,19 @@
httpd_exit(1);

/* change our root directory if specified */
- if (httpd_chroot_flag && (httpd_chroot() == -1))
+ if ((httpd_flags & HTTPD_FLAG_CHROOT) && (httpd_chroot() == -1))
httpd_exit(1);

- if (httpd_writepid() == -1)
+ if ((httpd_flags & HTTPD_FLAG_PIDFILE) &&
+ (httpd_pidfile_create() == -1))
httpd_exit(1);

/* open the local control socket */
- if (httpd_ctl_open() == -1)
+ if ((httpd_flags & HTTPD_FLAG_CTLSOCK) && (httpd_ctl_open() == -1))
httpd_exit(1);

/* become a daemon */
- if ((httpd_daemon == TRUE) && (httpd_daemonize() == -1)) {
+ if ((httpd_flags & HTTPD_FLAG_DAEMON) && (httpd_daemonize() == -1)) {
httpd_log_err("failed to become a daemon");
httpd_exit(1);
}
@@ -384,6 +380,8 @@
{
cnp_logmask_t mask;

+ httpd_flags |= HTTPD_FLAG_DEBUG;
+
cnp_log_getmask(httpd_logchan, &mask);
CNP_LOG_UNMASKPRI(mask, CNP_LOG_DEBUG);
cnp_log_setmask(httpd_logchan, mask);
@@ -397,7 +395,6 @@
cnp_log_setmask(cnp_logchan, mask);

return (0);
-
}

/*
@@ -410,6 +407,8 @@
{
cnp_logmask_t mask;

+ httpd_flags |= HTTPD_FLAG_TRACE;
+
cnp_log_getmask(httpd_logchan, &mask);
CNP_LOG_UNMASKPRI(mask, CNP_LOG_TRACE);
cnp_log_setmask(httpd_logchan, mask);
@@ -427,6 +426,9 @@
/*
* httpd_daemonize()
*
+ * Turn the calling process into a daemon.
+ *
+ * Returns 0 on success, or -1 on failure.
*/
static int
httpd_daemonize(void)
@@ -469,6 +471,8 @@
/* update server's PID */
httpd_pid = getpid();

+ httpd_flags |= HTTPD_FLAG_IS_DAEMON;
+
return (0);
}

@@ -504,21 +508,22 @@
}

/*
- * httpd_writepid()
+ * httpd_pidfile_create()
*
* Write the process ID of the server into the file specified in <path>.
* The file is automatically deleted on a call to httpd_exit().
*/
static int
-httpd_writepid(void)
+httpd_pidfile_create(void)
{
int fd;
ssize_t ret;
char pidbuf[8];

+ httpd_log_info("creating PID file %s", httpd_pidfile_path);
if ((fd = open(httpd_pidfile_path, O_CREAT | O_EXCL| O_WRONLY,
httpd_pidfile_mode)) == -1) {
- httpd_log_errno("failed to open %s",
+ httpd_log_errno("failed to create PID file %s",
httpd_pidfile_path);
return (-1);
}
@@ -543,6 +548,65 @@

(void)close(fd);

+ httpd_flags |= HTTPD_FLAG_PIDFILE_CREATED;
+
+ return (0);
+}
+
+/*
+ * httpd_pidfile_unlink()
+ *
+ * Unlink the PID file that was created by an instance of the server upon
+ * startup. If the server does not have privileges to unlink the file, or
if
+ * the PID file contains the process ID of another process, it is not
unlinked
+ * and an error is reported.
+ *
+ * Returns 0 on success, or -1 on failure.
+ */
+static int
+httpd_pidfile_unlink(void)
+{
+ pid_t pid;
+ struct stat st;
+ FILE *pidfile;
+
+ httpd_log_trace("checking status of PID file %s", httpd_pidfile_path);
+ if (stat(httpd_pidfile_path, &st) == -1) {
+ httpd_log_errno("failed to stat PID file %s",
+ httpd_pidfile_path);
+ return (-1);
+ }
+
+ /*
+ * Check if the PID file contains our PID, otherwise it belongs to
+ * another instance, or was left there after a server crash.
+ */
+ if ((pidfile = fopen(httpd_pidfile_path, "r")) == NULL) {
+ httpd_log_errno("failed to open PID file %s",
+ httpd_pidfile_path);
+ return (-1);
+ }
+
+ if (fscanf(pidfile, "%d", &pid) != 1) {
+ httpd_log_errno("failed to read PID from PID file %s",
+ httpd_pidfile_path);
+ (void)fclose(pidfile);
+ return (-1);
+ }
+
+ if (pid != httpd_pid) {
+ httpd_log_warn("PID file %s belongs to another process",
+ httpd_pidfile_path);
+ return (-1);
+ }
+
+ httpd_log_info("unlinking PID file %s", httpd_pidfile_path);
+ if (unlink(httpd_pidfile_path) == -1) {
+ httpd_log_errno("failed to unlink PID file %s",
+ httpd_pidfile_path);
+ return (-1);
+ }
+
return (0);
}

@@ -556,15 +620,21 @@
static int
httpd_chroot(void)
{
- httpd_log_info("changing root directory to %s", httpd_chroot_path);
-
- (void)chdir(httpd_chroot_path);
+ httpd_log_notice("changing root directory to %s", httpd_chroot_path);
+
+ if (chdir(httpd_chroot_path) == -1) {
+ httpd_log_errno("failed to change current directory to %s",
+ httpd_chroot_path);
+ return (-1);
+ }

if (chroot(httpd_chroot_path) == -1) {
httpd_log_errno("failed to change root directory "
"to %s", httpd_chroot_path);
return (-1);
}
+
+ httpd_flags |= HTTPD_FLAG_IS_CHROOTED;

return (0);
}
@@ -581,8 +651,7 @@
char sigbuf[16], source[32];
struct timespec timeout;

- httpd_log_notice("%s ready to answer requests",
- httpd_server_ident);
+ httpd_log_notice("%s ready to answer requests", httpd_server_ident);

for (;;) {
if (httpd_restart_flag != 0) {
@@ -648,14 +717,10 @@
void
httpd_exit(int code)
{
- struct stat st;
-
- if (stat(httpd_pidfile_path, &st) == 0) {
- httpd_log_debug("unlinking PID file %s", httpd_pidfile_path);
- if (unlink(httpd_pidfile_path) == -1)
- httpd_log_errno("failed to unlink PID file %s",
- httpd_pidfile_path);
- }
+ (void)httpd_subsys_setlevel(HTTPD_SUBSYS_INITLEVEL);
+
+ if (httpd_flags & HTTPD_FLAG_PIDFILE_CREATED)
+ (void)httpd_pidfile_unlink();

if (httpd_acls != NULL) {
http_acl_free(httpd_acls);
=======================================
--- /trunk/src/sbin/httpd/httpd.h Thu Apr 1 15:35:10 2010
+++ /trunk/src/sbin/httpd/httpd.h Mon Nov 29 20:18:37 2010
@@ -81,6 +81,18 @@
#define HTTPD_CONN_MAX 128


+#define HTTPD_FLAG_VERBOSE 0x0001
+#define HTTPD_FLAG_DEBUG 0x0002
+#define HTTPD_FLAG_TRACE 0x0004
+#define HTTPD_FLAG_SYSLOG 0x0008
+#define HTTPD_FLAG_DAEMON 0x0010
+#define HTTPD_FLAG_IS_DAEMON 0x0020
+#define HTTPD_FLAG_CHROOT 0x0040
+#define HTTPD_FLAG_IS_CHROOTED 0x0080
+#define HTTPD_FLAG_PIDFILE 0x0100
+#define HTTPD_FLAG_PIDFILE_CREATED 0x0200
+#define HTTPD_FLAG_CTLSOCK 0x0400
+#define HTTPD_FLAG_CTLSOCK_CREATED 0x0800


/* phases */
=======================================
--- /trunk/src/sbin/httpd/log.c Tue May 11 18:09:42 2010
+++ /trunk/src/sbin/httpd/log.c Mon Nov 29 20:18:37 2010
@@ -54,8 +54,6 @@
cnp_log_t *httpd_logchan;


-int httpd_log_usesyslog = 0;
-

static const int httpd_log_syslog_primap[] = {
LOG_DEBUG,
@@ -136,14 +134,14 @@
if ((sp = strchr(timebuf, 'T')) != NULL)
*sp = ' ';

- if (httpd_daemon < 2) {
+ if (!(httpd_flags & HTTPD_FLAG_IS_DAEMON)) {
out = (msg->msg_priority >= CNP_LOG_WARN) ? stderr : stdout;

fprintf(out, "%s %s: %s\n", timebuf, __progname,
msg->msg_string);
}

- if (httpd_log_usesyslog) {
+ if (httpd_flags & HTTPD_FLAG_SYSLOG) {
syslog(httpd_log_syslog_primap[msg->msg_priority],
"%s", msg->msg_string);
}
=======================================
--- /trunk/src/sbin/httpd/private.h Sun Apr 25 14:53:59 2010
+++ /trunk/src/sbin/httpd/private.h Mon Nov 29 20:18:37 2010
@@ -73,6 +73,7 @@


extern volatile sig_atomic_t httpd_state;
+extern volatile sig_atomic_t httpd_flags;


void httpd_shutdown (int, int);

Reply all
Reply to author
Forward
0 new messages