/usr/share/doc/libapache2-mod-perl2-doc/docs/2.0/api/Apache2/ServerUtil.html is in libapache2-mod-perl2-doc 2.0.10-2ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 | <?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:root@localhost" />
</head>
<body>
<ul id="index">
<li><a href="#NAME">NAME</a></li>
<li><a href="#Synopsis">Synopsis</a></li>
<li><a href="#Description">Description</a></li>
<li><a href="#Methods-API">Methods API</a>
<ul>
<li><a href="#add_config">add_config</a></li>
<li><a href="#add_version_component">add_version_component</a></li>
<li><a href="#dir_config">dir_config</a></li>
<li><a href="#exists_config_define">exists_config_define</a></li>
<li><a href="#get_handlers">get_handlers</a></li>
<li><a href="#get_server_built">get_server_built</a></li>
<li><a href="#get_server_version">get_server_version</a></li>
<li><a href="#get_server_banner">get_server_banner</a></li>
<li><a href="#get_server_description">get_server_description</a></li>
<li><a href="#group_id">group_id</a></li>
<li><a href="#is_perl_option_enabled">is_perl_option_enabled</a></li>
<li><a href="#method_register">method_register</a></li>
<li><a href="#push_handlers">push_handlers</a></li>
<li><a href="#restart_count">restart_count</a></li>
<li><a href="#server">server</a></li>
<li><a href="#server_root">server_root</a></li>
<li><a href="#server_root_relative">server_root_relative</a></li>
<li><a href="#server_shutdown_cleanup_register">server_shutdown_cleanup_register</a></li>
<li><a href="#set_handlers">set_handlers</a></li>
<li><a href="#user_id">user_id</a></li>
</ul>
</li>
<li><a href="#Unsupported-API">Unsupported API</a>
<ul>
<li><a href="#error_log2stderr">error_log2stderr</a></li>
</ul>
</li>
<li><a href="#See-Also">See Also</a></li>
<li><a href="#Copyright">Copyright</a></li>
<li><a href="#Authors">Authors</a></li>
</ul>
<h1 id="NAME">NAME</h1>
<p>Apache2::ServerUtil - Perl API for Apache server record utils</p>
<h1 id="Synopsis">Synopsis</h1>
<pre><code> use Apache2::ServerUtil ();
$s = Apache2::ServerUtil->server;
# push config
$s->add_config(['ServerTokens off']);
# add components to the Server signature
$s->add_version_component("MyModule/1.234");
# access PerlSetVar/PerlAddVar values
my $srv_cfg = $s->dir_config;
# check command line defines
print "this is mp2"
if Apache2::ServerUtil::exists_config_define('MODPERL2');
# get PerlChildExitHandler configured handlers
@handlers = @{ $s->get_handlers('PerlChildExitHandler') || []};
# server build and version info:
$when_built = Apache2::ServerUtil::get_server_built();
$description = Apache2::ServerUtil::get_server_description();
$version = Apache2::ServerUtil::get_server_version();
$banner = Apache2::ServerUtil::get_server_banner();
# ServerRoot value
$server_root = Apache2::ServerUtil::server_root();
# get 'conf/' dir path (avoid using this function!)
my $dir = Apache2::ServerUtil::server_root_relative($r->pool, 'conf');
# set child_exit handlers
$r->set_handlers(PerlChildExitHandler => \&handler);
# server level PerlOptions flags lookup
$s->push_handlers(ChildExit => \&child_exit)
if $s->is_perl_option_enabled('ChildExit');
# extend HTTP to support a new method
$s->method_register('NEWGET');
# register server shutdown callback
Apache2::ServerUtil::server_shutdown_register_cleanup(sub { Apache2::Const::OK });
# do something only when the server restarts
my $cnt = Apache2::ServerUtil::restart_count();
do_something_once() if $cnt > 1;
# get the resolved ids from Group and User entries
my $user_id = Apache2::ServerUtil->user_id;
my $group_id = Apache2::ServerUtil->group_id;</code></pre>
<h1 id="Description">Description</h1>
<p><code>Apache2::ServerUtil</code> provides the <a>Apache server object</a> utilities API.</p>
<h1 id="Methods-API">Methods API</h1>
<p><code>Apache2::ServerUtil</code> provides the following functions and/or methods:</p>
<h2 id="add_config"><code>add_config</code></h2>
<p>Dynamically add Apache configuration:</p>
<pre><code> $s->add_config($lines);</code></pre>
<dl>
<dt id="obj:-s-Apache2::ServerRec-object">obj: <code>$s</code> ( <code><a>Apache2::ServerRec object</a></code> )</dt>
<dd>
</dd>
<dt id="arg1:-lines-ARRAY-ref">arg1: <code>$lines</code> ( ARRAY ref )</dt>
<dd>
<p>An ARRAY reference containing configuration lines per element, without the new line terminators.</p>
</dd>
<dt id="ret:-no-return-value">ret: no return value</dt>
<dd>
</dd>
<dt id="since:-2.0.00">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<p>See also: <code><a>$r->add_config</a></code></p>
<p>For example:</p>
<p>Add a configuration section at the server startup (e.g. from <i>startup.pl</i>):</p>
<pre><code> use Apache2::ServerUtil ();
my $conf = <<'EOC';
PerlModule Apache2::MyExample
<Location /perl>
SetHandler perl-script
PerlResponseHandler Apache2::MyExample
</Location>
EOC
Apache2::ServerUtil->server->add_config([split /\n/, $conf]);</code></pre>
<h2 id="add_version_component"><code>add_version_component</code></h2>
<p>Add a component to the version string</p>
<pre><code> $s->add_version_component($component);</code></pre>
<dl>
<dt id="obj:-s-Apache2::ServerRec-object1">obj: <code>$s</code> ( <code><a>Apache2::ServerRec object</a></code> )</dt>
<dd>
</dd>
<dt id="arg1:-component-string">arg1: <code>$component</code> ( string )</dt>
<dd>
<p>The string component to add</p>
</dd>
<dt id="ret:-no-return-value1">ret: no return value</dt>
<dd>
</dd>
<dt id="since:-2.0.001">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<p>This function is usually used by modules to advertise themselves to the world. It's picked up by such statistics collectors, like netcraft.com, which accomplish that by connecting to various servers and grabbing the server version response header (<code>Server</code>). Some servers choose to fully or partially conceal that header.</p>
<p>This method should be invoked in the <code><a>PerlPostConfigHandler</a></code> phase, which will ensure that the Apache core version number will appear first.</p>
<p>For example let's add a component <i>"Hikers, Inc/0.99999"</i> to the server string at the server startup:</p>
<pre><code> use Apache2::ServerUtil ();
use Apache2::Const -compile => 'OK';
Apache2::ServerUtil->server->push_handlers(
PerlPostConfigHandler => \&add_my_version);
sub add_my_version {
my ($conf_pool, $log_pool, $temp_pool, $s) = @_;
$s->add_version_component("Hikers, Inc/0.99999");
return Apache2::Const::OK;
}</code></pre>
<p>or of course you could register the <code><a>PerlPostConfigHandler</a></code> handler directly in <i>httpd.conf</i></p>
<p>Now when the server starts, you will something like:</p>
<pre><code> [Thu Jul 15 12:15:28 2004] [notice] Apache/2.0.51-dev (Unix)
mod_perl/1.99_15-dev Perl/v5.8.5 Hikers, Inc/0.99999
configured -- resuming normal operations</code></pre>
<p>Also remember that the <code>ServerTokens</code> directive value controls whether the component information is displayed or not.</p>
<h2 id="dir_config"><code>dir_config</code></h2>
<p><code>$s->dir_config()</code> provides an interface for the per-server variables specified by the <code>PerlSetVar</code> and <code>PerlAddVar</code> directives, and also can be manipulated via the <code><a>APR::Table</a></code> methods.</p>
<pre><code> $table = $s->dir_config();
$value = $s->dir_config($key);
@values = $s->dir_config->get($key);
$s->dir_config($key, $val);</code></pre>
<dl>
<dt id="obj:-s-Apache2::ServerRec-object2">obj: <code>$s</code> ( <code><a>Apache2::ServerRec object</a></code> )</dt>
<dd>
</dd>
<dt id="opt-arg2:-key-string">opt arg2: <code>$key</code> ( string )</dt>
<dd>
<p>Key string</p>
</dd>
<dt id="opt-arg3:-val-string">opt arg3: <code>$val</code> ( string )</dt>
<dd>
<p>Value string</p>
</dd>
<dt id="ret">ret: ...</dt>
<dd>
<p>Depends on the passed arguments, see further discussion</p>
</dd>
<dt id="since:-2.0.002">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<p>The keys are case-insensitive.</p>
<pre><code> $t = $s->dir_config();</code></pre>
<p>dir_config() called in a scalar context without the <code>$key</code> argument returns a <i>HASH</i> reference blessed into the <i>APR::Table</i> class. This object can be manipulated via the <i>APR::Table</i> methods. For available methods see <i>APR::Table</i>.</p>
<pre><code> @values = $s->dir_config->get($key);</code></pre>
<p>To receive a list of values you must use <code>get()</code> method from the <code><a>APR::Table</a></code> class.</p>
<pre><code> $value = $s->dir_config($key);</code></pre>
<p>If the <code>$key</code> argument is passed in the scalar context only a single value will be returned. Since the table preserves the insertion order, if there is more than one value for the same key, the oldest value assosiated with the desired key is returned. Calling in the scalar context is also much faster, as it'll stop searching the table as soon as the first match happens.</p>
<pre><code> $s->dir_config($key => $val);</code></pre>
<p>If the <code>$key</code> and the <code>$val</code> arguments are used, the set() operation will happen: all existing values associated with the key <code>$key</code> (and the key itself) will be deleted and <code>$value</code> will be placed instead.</p>
<pre><code> $s->dir_config($key => undef);</code></pre>
<p>If <code>$val</code> is <i>undef</i> the unset() operation will happen: all existing values associated with the key <code>$key</code> (and the key itself) will be deleted.</p>
<h2 id="exists_config_define"><code>exists_config_define</code></h2>
<p>Check for a definition from the server startup command line (e.g. <code>-DMODPERL2</code>)</p>
<pre><code> $result = Apache2::ServerUtil::exists_config_define($name);</code></pre>
<dl>
<dt id="arg1:-name-string">arg1: <code>$name</code> ( string )</dt>
<dd>
<p>The define string to check for</p>
</dd>
<dt id="ret:-result-boolean">ret: <code>$result</code> ( boolean )</dt>
<dd>
<p>true if defined, false otherwise</p>
</dd>
<dt id="since:-2.0.003">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<p>For example:</p>
<pre><code> print "this is mp2"
if Apache2::ServerUtil::exists_config_define('MODPERL2');</code></pre>
<h2 id="get_handlers"><code>get_handlers</code></h2>
<p>Returns a reference to a list of handlers enabled for a given phase.</p>
<pre><code> $handlers_list = $s->get_handlers($hook_name);</code></pre>
<dl>
<dt id="obj:-s-Apache2::ServerRec-object3">obj: <code>$s</code> ( <code><a>Apache2::ServerRec object</a></code> )</dt>
<dd>
</dd>
<dt id="arg1:-hook_name-string">arg1: <code>$hook_name</code> ( string )</dt>
<dd>
<p>a string representing the phase to handle.</p>
</dd>
<dt id="ret:-handlers_list-ref-to-an-ARRAY-of-CODE-refs">ret: <code>$handlers_list</code> (ref to an ARRAY of CODE refs)</dt>
<dd>
<p>a list of references to the handler subroutines</p>
</dd>
<dt id="since:-2.0.004">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<p>See also: <code><a>$r->add_config</a></code></p>
<p>For example:</p>
<p>A list of handlers configured to run at the <i>child_exit</i> phase:</p>
<pre><code> @handlers = @{ $s->get_handlers('PerlChildExitHandler') || []};</code></pre>
<h2 id="get_server_built"><code>get_server_built</code></h2>
<p>Get the date and time that the server was built</p>
<pre><code> $when_built = Apache2::ServerUtil::get_server_built();</code></pre>
<dl>
<dt id="ret:-when_built-string">ret: <code>$when_built</code> ( string )</dt>
<dd>
<p>The server build time string</p>
</dd>
<dt id="since:-2.0.005">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<h2 id="get_server_version"><code>get_server_version</code></h2>
<p>Get the server version string</p>
<pre><code> $version = Apache2::ServerUtil::get_server_version();</code></pre>
<dl>
<dt id="ret:-version-string">ret: <code>$version</code> ( string )</dt>
<dd>
<p>The server version string</p>
</dd>
<dt id="since:-2.0.006">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<h2 id="get_server_banner"><code>get_server_banner</code></h2>
<p>Get the server banner</p>
<pre><code> $banner = Apache2::ServerUtil::get_server_banner();</code></pre>
<dl>
<dt id="ret:-banner-string">ret: <code>$banner</code> ( string )</dt>
<dd>
<p>The server banner</p>
</dd>
<dt id="since:-2.0.4">since: 2.0.4</dt>
<dd>
</dd>
</dl>
<h2 id="get_server_description"><code>get_server_description</code></h2>
<p>Get the server description</p>
<pre><code> $description = Apache2::ServerUtil::get_server_description();</code></pre>
<dl>
<dt id="ret:-description-string">ret: <code>$description</code> ( string )</dt>
<dd>
<p>The server description</p>
</dd>
<dt id="since:-2.0.41">since: 2.0.4</dt>
<dd>
</dd>
</dl>
<h2 id="group_id"><code>group_id</code></h2>
<p>Get the group id corresponding to the <code>Group</code> directive in <i>httpd.conf</i>:</p>
<pre><code> $gid = Apache2::ServerUtil->group_id;</code></pre>
<dl>
<dt id="obj:-Apache2::ServerUtil-class-name">obj: <code>Apache2::ServerUtil</code> (class name)</dt>
<dd>
</dd>
<dt id="ret:-gid-integer">ret: <code>$gid</code> ( integer )</dt>
<dd>
<p>On Unix platforms returns the gid corresponding to the value used in the <code>Group</code> directive in <i>httpd.conf</i>. On other platforms returns 0.</p>
</dd>
<dt id="since:-2.0.03">since: 2.0.03</dt>
<dd>
</dd>
</dl>
<h2 id="is_perl_option_enabled"><code>is_perl_option_enabled</code></h2>
<p>check whether a server level <code>PerlOptions</code> flag is enabled or not.</p>
<pre><code> $result = $s->is_perl_option_enabled($flag);</code></pre>
<dl>
<dt id="obj:-s-Apache2::ServerRec-object4">obj: <code>$s</code> ( <code><a>Apache2::ServerRec object</a></code> )</dt>
<dd>
</dd>
<dt id="arg1:-flag-string">arg1: <code>$flag</code> ( string )</dt>
<dd>
</dd>
<dt id="ret:-result-boolean1">ret: <code>$result</code> ( boolean )</dt>
<dd>
</dd>
<dt id="since:-2.0.007">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<p>For example to check whether the <code>ChildExit</code> hook is enabled (which can be disabled with <code>PerlOptions -ChildExit</code>) and configure some handlers to run if enabled:</p>
<pre><code> $s->push_handlers(ChildExit => \&child_exit)
if $s->is_perl_option_enabled('ChildExit');</code></pre>
<p>See also: <a>PerlOptions</a> and <a>the equivalent function for directory level PerlOptions flags</a>.</p>
<h2 id="method_register"><code>method_register</code></h2>
<p>Register a new request method, and return the offset that will be associated with that method.</p>
<pre><code> $offset = $s->method_register($methname);</code></pre>
<dl>
<dt id="obj:-s-Apache2::ServerRec-object5">obj: <code>$s</code> ( <code><a>Apache2::ServerRec object</a></code> )</dt>
<dd>
</dd>
<dt id="arg1:-methname-string">arg1: <code>$methname</code> ( string )</dt>
<dd>
<p>The name of the new method to register (in addition to the already supported <code>GET</code>, <code>HEAD</code>, etc.)</p>
</dd>
<dt id="ret:-offset-integer">ret: <code>$offset</code> ( integer )</dt>
<dd>
<p>An int value representing an offset into a bitmask. You can probably ignore it.</p>
</dd>
<dt id="since:-2.0.008">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<p>This method allows you to extend the HTTP protocol to support new methods, which fit the HTTP paradigm. Of course you will need to write a client that understands that protocol extension. For a good example, refer to the <code>MyApache2::SendEmail</code> example presented in <code><a>the PerlHeaderParserHandler section</a></code>, which demonstrates how a new method <code>EMAIL</code> is registered and used.</p>
<h2 id="push_handlers"><code>push_handlers</code></h2>
<p>Add one or more handlers to a list of handlers to be called for a given phase.</p>
<pre><code> $ok = $s->push_handlers($hook_name => \&handler);
$ok = $s->push_handlers($hook_name => [\&handler, \&handler2]);</code></pre>
<dl>
<dt id="obj:-s-Apache2::ServerRec-object6">obj: <code>$s</code> ( <code><a>Apache2::ServerRec object</a></code> )</dt>
<dd>
</dd>
<dt id="arg1:-hook_name-string1">arg1: <code>$hook_name</code> ( string )</dt>
<dd>
<p>the phase to add the handlers to</p>
</dd>
<dt id="arg2:-handlers-CODE-ref-or-SUB-name-or-an-ARRAY-ref">arg2: <code>$handlers</code> ( CODE ref or SUB name or an ARRAY ref )</dt>
<dd>
<p>a single handler CODE reference or just a name of the subroutine (fully qualified unless defined in the current package).</p>
<p>if more than one passed, use a reference to an array of CODE refs and/or subroutine names.</p>
</dd>
<dt id="ret:-ok-boolean">ret: <code>$ok</code> ( boolean )</dt>
<dd>
<p>returns a true value on success, otherwise a false value</p>
</dd>
<dt id="since:-2.0.009">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<p>See also: <code><a>$r->add_config</a></code></p>
<p>Examples:</p>
<p>A single handler:</p>
<pre><code> $s->push_handlers(PerlChildExitHandler => \&handler);</code></pre>
<p>Multiple handlers:</p>
<pre><code> $s->push_handlers(PerlChildExitHandler => ['Foo::Bar::handler', \&handler2]);</code></pre>
<p>Anonymous functions:</p>
<pre><code> $s->push_handlers(PerlLogHandler => sub { return Apache2::Const::OK });</code></pre>
<h2 id="restart_count"><code>restart_count</code></h2>
<p>How many times the server was restarted.</p>
<pre><code> $restart_count = Apache2::ServerUtil::restart_count();</code></pre>
<dl>
<dt id="ret:-restart_count-number">ret: <code>restart_count</code> ( number )</dt>
<dd>
</dd>
<dt id="since:-2.0.0010">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<p>The following demonstration should make it clear what values to expect from this function. Let's add the following code to <i>startup.pl</i>, so it's run every time <i>httpd.conf</i> is parsed:</p>
<pre><code> use Apache2::ServerUtil ();
my $cnt = Apache2::ServerUtil::restart_count();
open my $fh, ">>/tmp/out" or die "$!";
print $fh "cnt: $cnt\n";
close $fh;</code></pre>
<p>Now let's run a series of server starts and restarts and look at what is logged into <i>/tmp/out</i>:</p>
<pre><code> % httpd -k start
cnt: 1
cnt: 2
% httpd -k graceful
cnt: 1
cnt: 3
% httpd -k graceful
cnt: 1
cnt: 4
% httpd -k stop
cnt: 1</code></pre>
<p>Remembering that <a>Apache restarts itself immediately after starting</a>, we can see that the <code>restart_count</code> goes from 1 to 2 during the server start. Moreover we can see that every operation forces the parsing of <i>httpd.conf</i> and therefore reinitialization of mod_perl (and running all the code found in <i>httpd.conf</i>). This happens even when the server is shutdown via <code>httpd -k stop</code>.</p>
<p>What conclusions can be drawn from this demonstration:</p>
<ul>
<li><p><code>Apache2::ServerUtil::restart_count()</code> returns 1 every time some <code>-k</code> command is passed to Apache (or <code>kill -USR1</code> or some alternative signal is received).</p>
</li>
<li><p>At all other times the count will be 2 or higher. So for example on graceful restart the count will be 3 or higher.</p>
</li>
</ul>
<p>For example if you want to run something every time <code>httpd -k</code> is run you just need to check whether <code>restart_count()</code> returns 1:</p>
<pre><code> my $cnt = Apache2::ServerUtil::restart_count();
do_something() if $cnt == 1;</code></pre>
<p>To do something only when server restarts (<code>httpd -k start</code> or <code>httpd -k graceful)</code>, check whether <code>restart_count()</code> is bigger than 1:</p>
<pre><code> my $cnt = Apache2::ServerUtil::restart_count();
do_something() if $cnt > 1;</code></pre>
<h2 id="server"><code>server</code></h2>
<p>Get the main server's object</p>
<pre><code> $main_s = Apache2::ServerUtil->server();</code></pre>
<dl>
<dt id="obj:-Apache2::ServerUtil-class-name1">obj: <code>Apache2::ServerUtil</code> (class name)</dt>
<dd>
</dd>
<dt id="ret:-main_s-Apache2::ServerRec-object">ret: <code>$main_s</code> ( <code><a>Apache2::ServerRec object</a></code> )</dt>
<dd>
</dd>
<dt id="since:-2.0.0011">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<h2 id="server_root"><code>server_root</code></h2>
<p>returns the value set by the top-level <code>ServerRoot</code> directive.</p>
<pre><code> $server_root = Apache2::ServerUtil::server_root();</code></pre>
<dl>
<dt id="ret:-server_root-string">ret: <code>$server_root</code> ( string )</dt>
<dd>
</dd>
<dt id="since:-2.0.0012">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<h2 id="server_root_relative"><code>server_root_relative</code></h2>
<p>Returns the canonical form of the filename made absolute to <code>ServerRoot</code>:</p>
<pre><code> $path = Apache2::ServerUtil::server_root_relative($pool, $fname);</code></pre>
<dl>
<dt id="arg1:-pool-APR::Pool-object">arg1: <code>$pool</code> ( <code><a>APR::Pool object</a></code> )</dt>
<dd>
<p>Make sure that you read the following explanation and understand well which pool object you need to pass before using this function.</p>
</dd>
<dt id="opt-arg2:-fname-string">opt arg2: <code>$fname</code> ( string )</dt>
<dd>
</dd>
<dt id="ret:-path-string">ret: <code>$path</code> ( string )</dt>
<dd>
<p>The concatenation of <code>ServerRoot</code> and the <code>$fname</code>.</p>
<p>If <code>$fname</code> is not specified, the value of <code>ServerRoot</code> is returned with a trailing <code>/</code>. (it's the same as using <code>''</code> as <code>$fname</code>'s value).</p>
</dd>
<dt id="since:-2.0.0013">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<p><code>$fname</code> is appended to the value of <code>ServerRoot</code> and returned. For example:</p>
<pre><code> my $dir = Apache2::ServerUtil::server_root_relative($r->pool, 'logs');</code></pre>
<p>You must be extra-careful when using this function. If you aren't sure what you are doing don't use it.</p>
<p>It's much safer to build the path by yourself using use <code><a href="#C_Apache2__server_root_">Apache2::ServerUtil::server_root()</a></code>, For example:</p>
<pre><code> use File::Spec::Functions qw(catfile);
my $path = catfile Apache2::ServerUtil::server_root, qw(t logs);</code></pre>
<p>In this example, no memory allocation happens on the Apache-side and you aren't risking to get a memory leak.</p>
<p>The problem with <code>server_root_relative</code> is that Apache allocates memory to concatenate the path string. The memory is allocated from the pool object. If you call this method on the server pool object it'll allocate the memory from it. If you do that at the server startup, it's perfectly right, since you will do that only once. However if you do that from within a request or a connection handler, you create a memory leak every time it is called -- as the memory gets allocated from the server pool, it will be freed only when the server is shutdown. Therefore if you need to build a relative to the root server path for the duration of the request, use the request pool:</p>
<pre><code> use Apache2::RequestRec ();
Apache2::ServerUtil::server_root_relative($r->pool, $fname);</code></pre>
<p>If you need to have the path for the duration of a connection (e.g. inside a protocol handler), you should use:</p>
<pre><code> use Apache2::Connection ();
Apache2::ServerUtil::server_root_relative($c->pool, $fname);</code></pre>
<p>And if you want it for the scope of the server file:</p>
<pre><code> use Apache2::Process ();
use Apache2::ServerUtil ();
Apache2::ServerUtil::server_root_relative($s->process->pool, $fname);</code></pre>
<p>Moreover, you could have encountered the opposite problem, where you have used a short-lived pool object to construct the path, but tried to use the resulting path variable, when that pool has been destructed already. In order to avoid mysterious segmentation faults, mod_perl does a wasteful copy of the path string when returning it to you -- another reason to avoid using this function.</p>
<h2 id="server_shutdown_cleanup_register"><code>server_shutdown_cleanup_register</code></h2>
<p>Register server shutdown cleanup callback:</p>
<pre><code> Apache2::ServerUtil::server_shutdown_cleanup_register($sub);</code></pre>
<dl>
<dt id="arg1:-sub-CODE-ref-or-SUB-name">arg1: <code>$sub</code> ( CODE ref or SUB name )</dt>
<dd>
</dd>
<dt id="ret:-no-return-value2">ret: no return value</dt>
<dd>
</dd>
<dt id="since:-2.0.0014">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<p>This function can be used to register a callback to be run once at the server shutdown (compared to <code><a>PerlChildExitHandler</a></code> which will execute the callback for each exiting child process).</p>
<p>For example in order to arrange the function <code>do_my_cleanups()</code> to be run every time the server shuts down (or restarts), run the following code at the server startup:</p>
<pre><code> Apache2::ServerUtil::server_shutdown_cleanup_register(\&do_my_cleanups);</code></pre>
<p>It's necessary to run this code at the server startup (normally <i>startup.pl</i>). The function will croak if run after the <code><a>PerlPostConfigHandler</a></code> phase.</p>
<p>Values returned from cleanup functions are ignored. If a cleanup dies the exception is stringified and passed to <code>warn()</code>. Usually, this results in printing it to the <i>error_log</i>.</p>
<h2 id="set_handlers"><code>set_handlers</code></h2>
<p>Set a list of handlers to be called for a given phase. Any previously set handlers are forgotten.</p>
<pre><code> $ok = $s->set_handlers($hook_name => \&handler);
$ok = $s->set_handlers($hook_name => [\&handler, \&handler2]);
$ok = $s->set_handlers($hook_name => []);
$ok = $s->set_handlers($hook_name => undef);</code></pre>
<dl>
<dt id="obj:-s-Apache2::ServerRec-object7">obj: <code>$s</code> ( <code><a>Apache2::ServerRec object</a></code> )</dt>
<dd>
</dd>
<dt id="arg1:-hook_name-string2">arg1: <code>$hook_name</code> ( string )</dt>
<dd>
<p>the phase to set the handlers in</p>
</dd>
<dt id="arg2:-handlers-CODE-ref-or-SUB-name-or-an-ARRAY-ref1">arg2: <code>$handlers</code> ( CODE ref or SUB name or an ARRAY ref )</dt>
<dd>
<p>a reference to a single handler CODE reference or just a name of the subroutine (fully qualified unless defined in the current package).</p>
<p>if more than one passed, use a reference to an array of CODE refs and/or subroutine names.</p>
<p>if the argument is <code>undef</code> or <code>[]</code> the list of handlers is reset to zero.</p>
</dd>
<dt id="ret:-ok-boolean1">ret: <code>$ok</code> ( boolean )</dt>
<dd>
<p>returns a true value on success, otherwise a false value</p>
</dd>
<dt id="since:-2.0.0015">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<p>See also: <code><a>$r->add_config</a></code></p>
<p>Examples:</p>
<p>A single handler:</p>
<pre><code> $r->set_handlers(PerlChildExitHandler => \&handler);</code></pre>
<p>Multiple handlers:</p>
<pre><code> $r->set_handlers(PerlFixupHandler => ['Foo::Bar::handler', \&handler2]);</code></pre>
<p>Anonymous functions:</p>
<pre><code> $r->set_handlers(PerlLogHandler => sub { return Apache2::Const::OK });</code></pre>
<p>Reset any previously set handlers:</p>
<pre><code> $r->set_handlers(PerlCleanupHandler => []);</code></pre>
<p>or</p>
<pre><code> $r->set_handlers(PerlCleanupHandler => undef);</code></pre>
<h2 id="user_id"><code>user_id</code></h2>
<p>Get the user id corresponding to the <code>User</code> directive in <i>httpd.conf</i>:</p>
<pre><code> $uid = Apache2::ServerUtil->user_id;</code></pre>
<dl>
<dt id="obj:-Apache2::ServerUtil-class-name2">obj: <code>Apache2::ServerUtil</code> (class name)</dt>
<dd>
</dd>
<dt id="ret:-uid-integer">ret: <code>$uid</code> ( integer )</dt>
<dd>
<p>On Unix platforms returns the uid corresponding to the value used in the <code>User</code> directive in <i>httpd.conf</i>. On other platforms returns 0.</p>
</dd>
<dt id="since:-2.0.031">since: 2.0.03</dt>
<dd>
</dd>
</dl>
<h1 id="Unsupported-API">Unsupported API</h1>
<p><code>Apache2::ServerUtil</code> also provides auto-generated Perl interface for a few other methods which aren't tested at the moment and therefore their API is a subject to change. These methods will be finalized later as a need arises. If you want to rely on any of the following methods please contact the <a>the mod_perl development mailing list</a> so we can help each other take the steps necessary to shift the method to an officially supported API.</p>
<h2 id="error_log2stderr"><code>error_log2stderr</code></h2>
<p>Start sending STDERR to the error_log file</p>
<pre><code> $s->error_log2stderr();</code></pre>
<dl>
<dt id="obj:-s-Apache2::ServerRec-object8">obj: <code>$s</code> ( <code><a>Apache2::ServerRec object</a></code> )</dt>
<dd>
<p>The current server</p>
</dd>
<dt id="ret:-no-return-value3">ret: no return value</dt>
<dd>
</dd>
<dt id="since:-2.0.0016">since: 2.0.00</dt>
<dd>
</dd>
</dl>
<p>This method may prove useful if you want to start redirecting STDERR to the error_log file before Apache does that on the startup.</p>
<h1 id="See-Also">See Also</h1>
<p><a>mod_perl 2.0 documentation</a>.</p>
<h1 id="Copyright">Copyright</h1>
<p>mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0.</p>
<h1 id="Authors">Authors</h1>
<p><a>The mod_perl development team and numerous contributors</a>.</p>
</body>
</html>
|