lighttpd cgi不能重启

6 篇文章 0 订阅

1. 背景

cgi出现coredump后,lighttpd不能拉动cgi重启。

2. 重现问题

2.1. cgi实现

/*! cgi简单实现 */
#include <stdio.h>
#include <fcgiapp.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, const char *argv)
{
    while (1) {
        FCGX_Request request = {0};
        FCGX_InitRequest(&request, 0, 0);
        FCGX_Accept_r(&request);
        system("sleep 100000000");
        FCGX_Finish_r(&request);
    }

    return 0;
}

2.2. 场景重现

# 运行lighttpd,此处将worker线程配置为4个
/smbdir/third_party/sbin/lighttpd -f /smbdir/third_party/lighttpd/etc/lighttpd.conf -D

# 手动kill掉cgi会出现cgi不能正常启动的问题
killall -11 cgi

3. 原因

lighttpd利用mod_facgi模块实现与cgi的交互,而cgi利用标准输入与lighttpd之间的信息互通,当在cgi中执行shell命令时,子进程会继承父进程的一些属性(标准输入也会被继承)。

3.1. lighttpd判断cgi是否正在运行

    gw_fd = fdevent_socket_cloexec(proc->saddr->sa_family, SOCK_STREAM, 0);
    if (-1 == gw_fd) {
        log_perror(errh, __FILE__, __LINE__, "socket()");
        return -1;
    }

    do {
        status = connect(gw_fd, proc->saddr, proc->saddrlen);
        log_perror(errh, __FILE__, __LINE__, "status[%d], reason[%s], errno[%d]", status, strerror(errno), errno);
    } while (-1 == status && errno == EINTR);

    if (-1 == status && errno != ENOENT && proc->unixsocket) {
        log_perror(errh, __FILE__, __LINE__,
          "connect %s", proc->unixsocket->ptr);
        unlink(proc->unixsocket->ptr);
    }

    close(gw_fd);

如果此时cgi进程出现coredump,而由其产生的子进程由于继承了cgi的标准输入,会使上述connect系统调用正常返回0,从而导致lighttpd重启cgi进程失败

3.2. lighttpd重启cgi原理

int status;

/*! lighttpd管理进程会在此处监听子进程状态,如果有子进程终止运行,则fdevent_waitpid_intr会返回 */
if (-1 != (pid = fdevent_waitpid_intr(-1, &status))) {
	log_monotonic_secs = server_monotonic_secs();
	log_epoch_secs = server_epoch_secs(srv);

    /*! 子进程终止运行后,该函数会最终调用gw_spawn_connection重启cgi进程 */
	if (plugins_call_handle_waitpid(srv, pid, status) != HANDLER_GO_ON) {
		if (!timer) alarm((timer = 5));
		continue;
	}
	switch (fdlog_pipes_waitpid_cb(pid)) {
	  default: break;
	  case -1: if (!timer) alarm((timer = 5));
		   __attribute_fallthrough__
	  case  1: continue;
	}
	/** 
	 * check if one of our workers went away
	 */
	for (int n = 0; n < npids; ++n) {
		if (pid == pids[n]) {
			pids[n] = -1;
			num_childs++;
			break;
		}
	}
}

4. 解决方案

自行封装形如popen或system类的接口,内部使用execl系列函数执行shell命令。但在执行命令前优先关闭标准输入(close(0);)即可解决该问题。

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
lighttpd 是一个轻量级的 Web 服务器,它支持 CGI(公共网关接口)来实现动态内容的处理。CGI 是一种通用的协议,它允许 Web 服务器与外部程序(通常是用脚本语言编写的)进行交互,以生成动态的网页内容。 在 lighttpd 中启用 CGI 支持,你需要进行以下配置步骤: 1. 确保你已经安装了 lighttpd,并且已经启动了服务器。 2. 编辑 lighttpd 的配置文件,通常位于 /etc/lighttpd/lighttpd.conf。 3. 在配置文件中找到 server.modules 部分,并确保 mod_cgi 模块已经被加载。如果没有加载,你可以将类似以下的行添加到配置文件中: ``` server.modules += ("mod_cgi") ``` 4. 找到 server.document-root 部分,并设置网页文件的根目录。例如: ``` server.document-root = "/var/www/html" ``` 5. 找到 server.indexfiles 部分,并确保包含了适当的索引文件(例如 index.html 或 index.php)。 6. 找到 cgi.assign 部分,并添加以下行来指定 CGI 脚本的文件扩展名和解释器路径。例如: ``` cgi.assign = ( ".cgi" => "/usr/bin/perl", ".pl" => "/usr/bin/perl", ".php" => "/usr/bin/php-cgi" ) ``` 这个例子中,.cgi、.pl 和 .php 扩展名的脚本将分别由 Perl 和 PHP 解释器处理。 7. 保存并关闭配置文件。 8. 重新启动 lighttpd 服务器以使配置生效。 现在,你可以将你的 CGI 脚本放置在指定的网页根目录下,并通过浏览器访问它们。确保你的脚本有可执行权限,并且文件扩展名与配置文件中指定的一致。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值