1 /*-
2  * Copyright (c) 2011 Stanislav Sedov <stas@FreeBSD.org>
3  * Copyright (c) 2007 Hosting Telesystems, JSC
4  * All rights reserved.
5  *
6  * This software was developed by Stanislav Sedov <stas@FreeBSD.org>
7  * under contract to Hosting Telesystems, JSC.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <time.h>
36 
37 #include <assert.h>
38 
39 #include <sys/param.h>
40 
41 #include <apr.h>
42 #include <apr_strings.h>
43 #include <apr_hooks.h>
44 
45 #include <httpd.h>
46 #include <http_config.h>
47 #include <http_core.h>
48 #include <http_request.h>
49 
50 #undef PACKAGE_BUGREPORT /* damn apache */
51 #undef PACKAGE_NAME
52 #undef PACKAGE_STRING
53 #undef PACKAGE_TARNAME
54 #undef PACKAGE_VERSION
55 
56 #include "config.h"
57 
58 /*
59  * Prototypes
60  */
61 static void register_hooks		__P((apr_pool_t *p));
62 
63 /* Forward */
64 module AP_MODULE_DECLARE_DATA proctitle_module;
65 
66 unsigned int enabled;	/* enable/disable */
67 static char *urisep;
68 
69 /*
70  * Set process titles according to urls they're processing
71  */
72 static int
proctitle_fixup(request_rec * r)73 proctitle_fixup(request_rec *r) {
74 	const char *name;
75 
76 	name = ap_get_server_name(r);
77 	if (name != NULL && r->uri != NULL)
78 		setproctitle("-[busy] %s%s%s", name, urisep, r->uri);
79 
80 	return OK;
81 }
82 
83 static int
proctitle_log(request_rec * r)84 proctitle_log(request_rec *r) {
85 	const char *name;
86 
87 	name = ap_get_server_name(r);
88 	if (name != NULL && r->uri != NULL)
89 		setproctitle("-[idle] %s%s%s", name, urisep, r->uri);
90 
91 	return OK;
92 }
93 
94 /*
95  ********************* Standard module stuff ************************
96  */
97 
98 static const char *
proctitle_flag(cmd_parms * cmd __unused,void * mconfig __unused,int flag)99 proctitle_flag(cmd_parms *cmd __unused, void *mconfig __unused, int flag)
100 {
101 
102 	enabled = flag;
103 
104 	return NULL;
105 }
106 
107 static const char *
proctitle_str(cmd_parms * cmd,void * mconfig __unused,const char * arg)108 proctitle_str(cmd_parms *cmd, void *mconfig __unused, const char *arg)
109 {
110 	if (arg == NULL || cmd == NULL)
111 		return NULL;
112 
113 	urisep = apr_pstrdup(cmd->pool, arg);
114 }
115 
116 static void *
cfg_init(apr_pool_t * p,server_rec * s __unused)117 cfg_init(apr_pool_t *p, server_rec *s __unused)
118 {
119 	enabled = 1;
120 	urisep = apr_pstrdup(p, "::");
121 
122 	return NULL;
123 }
124 
125 static void
register_hooks(apr_pool_t * p __unused)126 register_hooks(apr_pool_t *p __unused) {
127 
128 	ap_hook_fixups(proctitle_fixup, NULL, NULL, \
129 		APR_HOOK_MIDDLE);
130 	ap_hook_log_transaction(proctitle_log,NULL,NULL,APR_HOOK_LAST);
131 }
132 
133 static const command_rec proctitle_commands[] =
134 {
135 	AP_INIT_FLAG("ProctitleEnable", proctitle_flag, NULL, RSRC_CONF, \
136 	    "Enable/disable setting process titles (on by default)"),
137 	AP_INIT_TAKE1("ProctitleUriSep", proctitle_str, NULL, RSRC_CONF, \
138 	    "Set URI separator ('::' by default)"),
139 	{NULL, {NULL}, NULL, 0, 0, NULL},
140 };
141 
142 module AP_MODULE_DECLARE_DATA proctitle_module = {
143 	STANDARD20_MODULE_STUFF,
144 	NULL,			/* dir config creater */
145 	NULL,			/* dir merger --- default is to override */
146 	cfg_init,			/* server config */
147 	NULL,			/* merge server configs */
148 	proctitle_commands,	/* command apr_table_t */
149 	register_hooks,		/* register hooks */
150 };
151