Signal-Exit 开源项目教程

Signal-Exit 开源项目教程

signal-exitwhen you want to fire an event no matter how a process exits.项目地址:https://gitcode.com/gh_mirrors/si/signal-exit

1. 项目的目录结构及介绍

Signal-Exit 项目的目录结构相对简单,主要包含以下几个部分:

signal-exit/
├── LICENSE
├── README.md
├── index.js
├── package.json
└── test/
    ├── basic.js
    └── signals.js
  • LICENSE: 项目的许可证文件。
  • README.md: 项目的说明文档,包含项目的基本信息和使用方法。
  • index.js: 项目的主文件,包含了主要的逻辑和功能。
  • package.json: 项目的配置文件,包含了项目的依赖、脚本等信息。
  • test/: 测试目录,包含了项目的测试文件。

2. 项目的启动文件介绍

项目的启动文件是 index.js,该文件主要负责捕获和处理进程的退出信号。以下是 index.js 的主要内容:

var nativeExit = process.exit
var signals = {}
var onSignal = function () {}

process.exit = function (code) {
  if (!signals[process.pid]) {
    nativeExit(code)
  }
}

process.once('exit', function (code) {
  if (signals[process.pid]) {
    onSignal(code, signals[process.pid])
  }
  delete signals[process.pid]
})

function on (signal, handler) {
  if (!signals[signal]) {
    signals[signal] = []
    trap(signal)
  }
  signals[signal].push(handler)
}

function trap (signal) {
  var sig = signal
  process.on(signal, function onexit () {
    onSignal(0, sig)
    nativeExit()
  })
}

module.exports = function (code, signal) {
  onSignal(code, signal)
}

module.exports.on = on
  • process.exit: 重写了 process.exit 方法,确保在捕获到信号时执行自定义的退出逻辑。
  • process.once('exit', ...): 监听进程的退出事件,执行自定义的退出处理逻辑。
  • on(signal, handler): 注册信号处理函数。
  • trap(signal): 捕获指定的信号并执行相应的处理逻辑。

3. 项目的配置文件介绍

项目的配置文件是 package.json,该文件包含了项目的基本信息和依赖配置。以下是 package.json 的主要内容:

{
  "name": "signal-exit",
  "version": "3.0.7",
  "description": "Execute a callback when the process exits",
  "main": "index.js",
  "scripts": {
    "test": "tap",
    "prepublish": "npm test"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/tapjs/signal-exit.git"
  },
  "keywords": [
    "signal",
    "exit",
    "trap",
    "catch"
  ],
  "author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/tapjs/signal-exit/issues"
  },
  "homepage": "https://github.com/tapjs/signal-exit",
  "devDependencies": {
    "tap": "^15.0.9"
  }
}
  • name: 项目的名称。
  • version: 项目的版本号。
  • description: 项目的描述。
  • main: 项目的入口文件。
  • scripts: 项目的脚本命令,如测试命令 npm test
  • repository: 项目的仓库地址。
  • keywords: 项目的关键词。
  • author: 项目的作者。
  • license: 项目的许可证。
  • bugs: 项目的 Bug 跟踪地址。
  • homepage: 项目的主页。
  • devDependencies: 项目的开发依赖。

以上是 Signal-Exit 开源项目的教程,包含了项目的目录结构、启动文件和配置文件的详细介绍。希望对您有所帮助!

signal-exitwhen you want to fire an event no matter how a process exits.项目地址:https://gitcode.com/gh_mirrors/si/signal-exit

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是iperf3的C代码,可以在GitHub上找到完整的代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <signal.h> #include <errno.h> #include <netdb.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include "iperf.h" #include "iperf_api.h" #include "iperf_locale.h" #include "units.h" #include "net.h" #include "timer.h" #include "tcp_window_size.h" #include "cjson.h" #ifdef HAVE_SSL #include <openssl/ssl.h> #include <openssl/err.h> #endif /* Forwards. */ static int iperf_run(struct iperf_test *test); /**************************************************************************/ int main(int argc, char **argv) { int i; struct iperf_test *test; int result; struct sigaction sa; char client_version[10] = IPERF_VERSION; setlocale(LC_ALL, ""); bindtextdomain("iperf3", LOCALEDIR); textdomain("iperf3"); /* Initialize settings. */ test = iperf_new_test(); /* Register a few signal handlers to make clean exit more likely. */ memset(&sa, 0, sizeof(sa)); sa.sa_handler = &iperf_got_sigint; sigaction(SIGINT, &sa, NULL); sa.sa_handler = &iperf_got_sigpipe; sigaction(SIGPIPE, &sa, NULL); /* Parse command line options, handling some options immediately. */ i = iperf_parse_arguments(test, argc, argv); if (i < 0) { fprintf(stderr, "%s", iperf_strerror(i)); exit(1); } if (test->version) { printf("%s\n", client_version); exit(0); } else if (test->help) { iperf_printf(test, "\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n", "Usage: iperf3 [-options] [-s|-c host] [options]\n", "Client/Server:", " -V, --version show version information and quit.", " -h, --help show this message and quit.", " -i, --interval n seconds between periodic bandwidth reports.", " -f, --format [kmKM] format to report: Kbits, Mbits, KBytes, MBytes", " -P, --parallel n number of parallel client streams to run."); iperf_printf(test, "\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n", "Client specific:", " -c, --client <host> run in client mode, connecting to <host>.", " -u, --udp use UDP rather than TCP.", " -b, --bitrate #[KMG][/#] target bitrate in bits/sec (0 for unlimited)", " (default 1 Mbit/sec for UDP, unlimited for TCP)", " -t, --time n time in seconds to transmit for (default 10 secs)", " -n, --bytes n number of bytes to transmit (instead of -t)", " -k, --blockcount #[KMG] number of blocks (packets) to transmit (instead of -t or -n)", " -l, --length #[KMG] length of buffer to read or write", " (default 128 KB for TCP, dynamic or 8 KB for UDP)", " -R, --reverse reverse the direction of a test (client sends, server receives)."); iperf_printf(test, "\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n", "Server specific:", " -s, --server run in server mode.", " -D, --daemon run the server as a daemon", " -I, --pidfile file write PID file (default /var/run/iperf3.pid)", " -1, --one-off handle one client connection then exit.", " -B, --bind <host> bind to a specific interface, e.g. eth0"); iperf_printf(test, "\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n", "JSON options:", " -J, --json output in JSON format", " --logfile f send output to a log file", "", "For more information and tuning options, see iperf3's man page.", "", "Please report bugs to https://github.com/esnet/iperf", ""); exit(0); } /* Check for version number consistency. */ if (test->protocol->id == Ptcp && test->version == 3 && test->tcp.omit_version) { i = 2; } else { i = 3; } if (strncmp(client_version, test->version_string, i) != 0) { iperf_printf(test, "warning: version number mismatch (client %s, server %s)\n", client_version, test->version_string); } /* Initialize IP protocol */ if (test->cookiefile) { test->cookie = iperf_load_cookie(test->cookiefile); if (test->cookie == NULL) { iperf_errexit(test, "unable to load cookie from file '%s'", test->cookiefile); } } if (test->reverse) test->settings->reverse = 1; if (test->protocol->id == Pudp) { if (test->settings->mss || test->settings->socket_bufsize) { iperf_printf(test, "warning: MSS and socket buffer size settings are not used in UDP mode.\n"); } if (test->settings->no_delay) { iperf_printf(test, "warning: the TCP_NODELAY option is not used in UDP mode.\n"); } if (test->settings->pmtu != -1) { iperf_printf(test, "warning: the PMTU option is not used in UDP mode.\n"); } } #ifdef HAVE_SSL /* Initialize SSL library */ if (test->protocol->id == Ptcp && test->settings->ssl) { SSL_load_error_strings(); SSL_library_init(); test->sslctx = SSL_CTX_new(TLS_client_method()); if (test->sslctx == NULL) { iperf_errexit(test, "failed to create SSL context\n"); } if (test->settings->ssl_cafile) { if (SSL_CTX_load_verify_locations(test->sslctx, test->settings->ssl_cafile, NULL) != 1) { SSL_CTX_free(test->sslctx); iperf_errexit(test, "failed to load CA certificates from %s\n", test->settings->ssl_cafile); } } if (test->settings->ssl_cert) { if (SSL_CTX_use_certificate_chain_file(test->sslctx, test->settings->ssl_cert) != 1) { SSL_CTX_free(test->sslctx); iperf_errexit(test, "failed to load SSL certificate from %s\n", test->settings->ssl_cert); } } if (test->settings->ssl_key) { if (SSL_CTX_use_PrivateKey_file(test->sslctx, test->settings->ssl_key, SSL_FILETYPE_PEM) != 1) { SSL_CTX_free(test->sslctx); iperf_errexit(test, "failed to load SSL key from %s\n", test->settings->ssl_key); } } SSL_CTX_set_verify(test->sslctx, SSL_VERIFY_PEER, NULL); } #endif /* Daemon mode. */ if (test->daemon) { if (daemon(0, 0) != 0) { iperf_errexit(test, "error - failed to become a daemon: %s\n", strerror(errno)); } if (test->pidfile) { FILE *f; f = fopen(test->pidfile, "w"); if (f == NULL) { iperf_errexit(test, "error - unable to write PID file '%s': %s\n", test->pidfile, strerror(errno)); } fprintf(f, "%d\n", getpid()); fclose(f); } } /* Ignore SIGPIPE to simplify error handling */ sa.sa_handler = SIG_IGN; sigaction(SIGPIPE, &sa, NULL); /* If we are doing a single client (-1) and it is a daemon, don't do -D again */ if (test->daemon && test->num_ostreams == 1) test->daemon = 0; /* Defer daemon mode until after the above check for single client + daemon */ if (test->daemon) { if (daemon(0, 0) != 0) { iperf_errexit(test, "error - failed to become a daemon: %s\n", strerror(errno)); } if (test->pidfile) { FILE *f; f = fopen(test->pidfile, "w"); if (f == NULL) { iperf_errexit(test, "error - unable to write PID file '%s': %s\n", test->pidfile, strerror(errno)); } fprintf(f, "%d\n", getpid()); fclose(f); } } /* Ignore SIGPIPE to simplify error handling */ sa.sa_handler = SIG_IGN; sigaction(SIGPIPE, &sa, NULL); /* Set up the output stream */ if (test->json_output) { test->outfile = json_get_output_stream(test->json_output_file); } else if (test->logfile) { test->outfile = fopen(test->logfile, "w"); if (test->outfile == NULL) { iperf_errexit(test, "error - unable to write log to file '%s': %s\n", test->logfile, strerror(errno)); } } else { test->outfile = stdout; } /* Start the client or server */ if (test->server) { if (test->daemon) { iperf_printf(test, "Server listening on port %d\n", test->server_port); } else { iperf_printf(test, "-----------------------------------------------------------\n"); iperf_printf(test, "Server listening on %s port %d\n", test->settings->domain == AF_INET6 ? "[::]" : "0.0.0.0", test->server_port); iperf_printf(test, "-----------------------------------------------------------\n"); } result = iperf_run_server(test); } else { if (test->daemon) { iperf_printf(test, "Client connecting to %s, TCP port %d\n", test->server_hostname, test->server_port); } else { if (test->reverse) iperf_printf(test, "-----------------------------------------------------------\n"); iperf_printf(test, "Client connecting to %s, %s port %d\n", test->server_hostname, test->protocol->name, test->server_port); if (test->reverse) iperf_printf(test, "-----------------------------------------------------------\n"); } result = iperf_run_client(test); } if (test->cookie) iperf_delete_cookie(test->cookie); #ifdef HAVE_SSL if (test->protocol->id == Ptcp && test->settings->ssl) { SSL_CTX_free(test->sslctx); } ERR_free_strings(); #endif iperf_free_test(test); return result; } static int iperf_run(struct iperf_test *test) { test->start_time = milliseconds(); test->next_time = test->start_time; test->bytes_sent = 0; test->blocks_sent = 0; test->retransmits = 0; if (test->server_hostname) { test->server_hostname_len = strlen(test->server_hostname); } if (test->bind_address) { test->bind_address_len = strlen(test->bind_address); } if (test->json_output) { cJSON *json_output = cJSON_CreateObject(); if (json_output == NULL) { iperf_errexit(test, "error - cJSON_CreateObject failed: %s\n", strerror(errno)); } cJSON_AddItemToObject(json_output, "start", cJSON_CreateNumber((double) test->start_time / 1000)); if (test->verbose) { cJSON_AddItemToObject(json_output, "verbose", cJSON_CreateNumber(1)); } cJSON_AddItemToObject(json_output, "system_info", cJSON_CreateObject()); iperf_json_printf(json_output, "version", "%s", test->version); iperf_json_printf(json_output, "system_info", "%s", get_system_info()); if (test->title) { iperf_json_printf(json_output, "title", "%s", test->title); } if (test->extra_data) { cJSON_AddItemToObject(json_output, "extra_data", cJSON_Parse(test->extra_data)); } test->json_start_time = milliseconds(); test->json_output_string = cJSON_Print(json_output); cJSON_Delete(json_output); if (test->json_output_string == NULL) { iperf_errexit(test, "error - cJSON_Print failed: %s\n", strerror(errno)); } } if (test->protocol->id == Ptcp) { if (test->settings->socket_bufsize) { if (iperf_set_tcp_windowsize(test) != 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } } if (test->reverse) { if (test->protocol->id == Ptcp) { if (iperf_create_streams(test, test->reverse) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } if (iperf_connect(test, test->reverse) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } else { if (iperf_create_streams(test, test->reverse) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } if (iperf_connect(test, test->reverse) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } if (test->json_output) { cJSON *json_output = cJSON_CreateObject(); if (json_output == NULL) { iperf_errexit(test, "error - cJSON_CreateObject failed: %s\n", strerror(errno)); } cJSON_AddItemToObject(json_output, "start", cJSON_CreateNumber((double) test->start_time / 1000)); if (test->verbose) { cJSON_AddItemToObject(json_output, "verbose", cJSON_CreateNumber(1)); } iperf_json_printf(json_output, "interval", "%d", test->settings->stats_interval); if (test->title) { iperf_json_printf(json_output, "title", "%s", test->title); } if (test->extra_data) { cJSON_AddItemToObject(json_output, "extra_data", cJSON_Parse(test->extra_data)); } iperf_json_printf(json_output, "start_connected", "%d", test->connected); cJSON_AddItemToObject(json_output, "intervals", cJSON_CreateArray()); test->json_output_string = cJSON_Print(json_output); cJSON_Delete(json_output); if (test->json_output_string == NULL) { iperf_errexit(test, "error - cJSON_Print failed: %s\n", strerror(errno)); } } if (test->protocol->id == Pudp) { if (iperf_udp_connect(test) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } if (test->reverse) { if (test->protocol->id == Ptcp) { if (iperf_listen(test) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } if (test->protocol->id == Pudp || test->protocol->id == Psctp) { if (iperf_run_server_udp(test) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } else { if (iperf_run_server_tcp(test) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } } else { if (test->protocol->id == Ptcp) { if (iperf_run_client_tcp(test) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } else { if (iperf_run_client_udp(test) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } } if (test->json_output) { cJSON *json_output = cJSON_CreateObject(); if (json_output == NULL) { iperf_errexit(test, "error - cJSON_CreateObject failed: %s\n", strerror(errno)); } cJSON_AddItemToObject(json_output, "start", cJSON_CreateNumber((double) test->start_time / 1000)); if (test->verbose) { cJSON_AddItemToObject(json_output, "verbose", cJSON_CreateNumber(1)); } iperf_json_printf(json_output, "interval", "%d", test->settings->stats_interval); if (test->title) { iperf_json_printf(json_output, "title", "%s", test->title); } if (test
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

侯霆垣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值