daemon.c

daemon.c

#include <unistd.h>
#include <signal.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <syslog.h>

void init_daemon(void)
{
    int pid;
    int i;
    if(pid=fork())
        exit(0);//是父进程,结束父进程
    else if(pid< 0)
        exit(1);//fork失败,退出
    //是第一子进程,后台继续执行
    setsid();//第一子进程成为新的会话组长和进程组长
    //并与控制终端分离
    if(pid=fork())
        exit(0);//是第一子进程,结束第一子进程
    else if(pid< 0)
        exit(1);//fork失败,退出
    //是第二子进程,继续
    //第二子进程不再是会话组长
    for(i=0;i< NOFILE;++i)//关闭打开的文件描述符
        close(i);
    //chdir("/tmp");//改变工作目录到/tmp
    umask(0);//重设文件创建掩模
    return;
}

void sig_term(int signo)
{
    if(signo==SIGTERM) /*catchedsignalsentbykill(1)command*/
    {
        syslog(LOG_INFO,"log4j-agent program terminated.");
        closelog();
        exit(0);
    }
}

int main(int argn, char** argv)
{
    extern int errno;
    if (argn>1) 
    {
        init_daemon();//初始化为Daemon
        openlog("log4j-agent", LOG_PID, LOG_USER);
        syslog(LOG_INFO, "log4j-agent started.");
        signal(SIGTERM,sig_term);/*arrangetocatchthesignal*/
        char *p[argn + 1];
        int j=0;
        for(j=0; j<argn+1; j++){
            p[j] = 0;
        }
        int i=1;
        for(i=1; i<argn; i++){
            p[i-1] = argv[i];
        }
        for(j=0; j<argn+1; j++){
            printf("%s \n", p[j]);
        }
        int ret = execvp(argv[1], p);
        if (ret<0){
            //syslog(LOG_ERR, "execl failed:%s");
            fprintf(stderr,"execl failed:%s", strerror(errno));
            return -1;
        }
    }else{
        printf("usage: ./daemon commond [argv] \n");
        printf("su as: ./daemon java -jar log4j-agent.jar -f conf/log4j.xml -p 4561 \n");
    }

}

build

gcc daemon.c -o daemon

Usage:

./daemon java -jar log4j-agent.jar -f conf/log4j.xml -p 4561

You can run any program you want int linux with this ‘daemon’.

在 Docker 的配置管理中,`daemon.conf` 和 `daemon.json` 是两个可能被混淆的文件,但它们的用途和优先级有明确区别。以下是详细解析和操作指南: --- ### **一、文件定义与优先级** | 文件名 | 用途 | 优先级 | 默认路径 | |-----------------|----------------------------------------------------------------------|--------|------------------------------| | `daemon.json` | **Docker 官方标准配置文件**,用于定义守护进程参数(如镜像源、存储驱动等) | 高 | `/etc/docker/daemon.json` | | `daemon.conf` | 非官方标准文件,可能是用户自定义或某些旧版工具生成的配置 | 低 | 任意路径(需手动指定) | **关键结论**: Docker **仅自动加载 `/etc/docker/daemon.json`**,其他位置的 `daemon.conf` 不会被默认识别。若需使用 `daemon.conf`,必须通过命令行参数 `--config` 显式指定。 --- ### **二、典型使用场景** #### **1. 正确使用 `daemon.json`(推荐)** ```json { "registry-mirrors": [ "https://docker.mirrors.ustc.edu.cn" ], "storage-driver": "overlay2", "insecure-registries": ["192.168.1.100:5000"] } ``` **操作步骤**: ```bash # 创建或覆盖配置文件 sudo tee /etc/docker/daemon.json <<EOF { "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"] } EOF # 重启 Docker 生效 sudo systemctl restart docker ``` #### **2. 强制使用 `daemon.conf`(非常规场景)** 若必须使用 `daemon.conf`,需通过 `--config` 参数启动 Docker: ```bash # 示例:启动时指定配置文件路径 sudo dockerd --config /path/to/daemon.conf ``` **配置文件内容示例**: ```ini # daemon.conf 通常为 INI 格式(非 JSON) [Daemon] registry-mirrors = ["https://docker.mirrors.ustc.edu.cn"] ``` --- ### **三、常见问题与排查** #### **1. 配置未生效的排查步骤** ```bash # 检查 Docker 实际加载的配置文件路径 sudo dockerd --debug 2>&1 | grep "Loading configuration" # 验证配置文件语法(仅针对 daemon.json) sudo jq . /etc/docker/daemon.json # 若报错则说明 JSON 格式错误 ``` #### **2. 混合使用两个文件的冲突处理** - **现象**:同时存在 `daemon.json` 和 `--config` 指定的文件时,**后者会覆盖前者**的冲突参数。 - **解决**: 统一使用 `daemon.json`,或通过 `include` 指令合并配置(需 Docker 18.09+ 支持): ```json { "include": "/etc/docker/daemon.conf" } ``` #### **3. 文件权限问题** ```bash # 确保 Docker 用户(如 root)有读取权限 sudo chmod 644 /etc/docker/daemon.json sudo chown root:root /etc/docker/daemon.json ``` --- ### **四、进阶配置技巧** #### **1. 多环境配置管理** - **开发环境**:使用 `daemon.json` 直接编辑。 - **生产环境**:通过 Ansible/Puppet 等工具动态生成 `daemon.json`。 #### **2. 配置热重载(无需重启 Docker)** ```bash # 修改配置后发送 SIGHUP 信号 sudo kill -SIGHUP $(cat /var/run/docker.pid) ``` **限制**:部分参数(如存储驱动)仍需重启生效。 #### **3. 调试配置加载** ```bash # 查看 Docker 启动时加载的所有配置源 sudo dockerd --debug 2>&1 | grep "Loading config file" ``` --- ### **五、完整操作示例** #### **场景**:将镜像源改为中科大镜像,并允许内网 HTTP 仓库 ```bash # 1. 创建/修改 daemon.json sudo bash -c 'cat > /etc/docker/daemon.json <<EOF { "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"], "insecure-registries": ["192.168.1.100:5000"] } EOF' # 2. 验证配置 sudo jq . /etc/docker/daemon.json # 3. 重启服务 sudo systemctl restart docker # 4. 检查生效情况 docker info | grep -E "Registry Mirrors|Insecure Registries" ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值