lighttpd cgi不能重启

1. 背景

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

2. 重现问题

2.1. cgi实现

cpp 复制代码
/*! 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. 场景重现

bash 复制代码
# 运行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是否正在运行

cpp 复制代码
    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原理

cpp 复制代码
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);)**即可解决该问题。

相关推荐
Web极客码6 分钟前
如何在WordPress登录页面添加隐藏或显示密码按钮
运维·服务器
QQ12154614686 分钟前
使用远程桌面连接Windows 2012 R2 Standard服务器报错:出现身份验证错误。要求的函数不受支持。这可能是由于CredSSP加密数据库修正。
服务器·windows·windows server
haluhalu.29 分钟前
[特殊字符] 深入理解Linux信号机制:信号的产生,保存和捕捉
linux·运维·服务器
JY.yuyu33 分钟前
Linux磁盘管理 / 硬盘分区、创建逻辑卷
linux·运维·服务器
翼龙云_cloud1 小时前
亚马逊云渠道商:AWS EC2 实战案例解析
服务器·云计算·aws
~黄夫人~1 小时前
Kubernetes Pod 初始化容器(InitContainer)起不来的排错思路
linux·运维·服务器
运维有小邓@2 小时前
如何在 Linux 中查看系统日志消息
linux·运维·服务器
m0_738120722 小时前
渗透测试——y0usef靶机渗透提权详细过程(插件伪造请求头)
服务器·网络·web安全·ssh·php
gaize12132 小时前
阿里云服务器用途配置选购指南与最新价格表
服务器·阿里云·云计算
天边一坨浮云2 小时前
Ubuntu(PC)遇到的各种问题-EXT4-fs(vdb): VFS: Can‘t find ext4 filesystem
linux·ubuntu