このページを編集する際は,編集に関する方針に従ってください.
概要
- Jabberd14-1.6.0/jabberd/config.cにて定義
* check the configuration if a pidfile should be written and write it * * If the pidfile already exists, the jabberd process is existed.
- 初期化時に呼ばれて、pidfileにpidを書き込む。
引数
* @param x the parsed configuration file
実装
static void show_pid(xmlnode x) { xmlnode pidfile; char *path; char pidstr[16]; int fd; pid_t pid;
pidfile = xmlnode_get_tag(x, "pidfile"); if(pidfile == NULL) return;
path = xmlnode_get_data(pidfile); if(path == NULL) { return; }
/* try to create pidfile */ fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600); if (fd < 0) { if (errno == EEXIST) { /* the file already exists */ char oldpid[32] = ""; ssize_t bytesread = 0;
/* check if the process is still running */ fd = open(path, O_RDONLY); if (fd < 0) { fprintf(stderr, "The pidfile %s already exists, and it cannot be opened for reading (%s). Exiting ...\n", path, strerror(errno)); _exit(1); }
bytesread = read(fd, oldpid, sizeof(oldpid)-1); if (bytesread < 0) { fprintf(stderr, "The pidfile %s already exists, but there is a problem reading its content (%s). Exiting ...\n", path, strerror(errno)); _exit(1); } else if (bytesread == 0) { fprintf(stderr, "The pidfile %s already exists, but it has no content. Deleting it ...\n", path); } else {
- pidfileが存在して中身がある場合
pid_t filepid = 0; int killres = 0;
oldpid[bytesread] = 0; filepid = j_atoi(oldpid, 0);
if (filepid == 0) { fprintf(stderr, "The pidfile %s already exists, but does not contain a PID (%s). Exiting ...\n", path, oldpid); _exit(1); }
- pidfileのプロセスのエラーチェックを行う。sigが0のときは、pidが適切な値かどうかをチェックするらしい。
出展はhpのマニュアルだけど。
killres = kill(filepid, 0);
- 初期化時にファイルが存在して(つまり、既にjabberdプロセスが動いていて)そのプロセスがゾンビだった場合、新たにjabberdプロセスを起動して大丈夫
if (killres == -1 && errno == ESRCH) { fprintf(stderr, "Stale pidfile %s found. No process with PID %i is running. Deleting pidfile ...\n", path, filepid); } else { fprintf(stderr, "A pidfile already exists at %s, containing the PID (%i) of a running process. Exiting ...\n", path, filepid); _exit(1); } }
- ファイルを消して、オープンしなおす
unlink(path); fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600); if (fd < 0) { fprintf(stderr, "Still having problems accessing pidfile %s: %s\n", path, strerror(errno)); _exit(1); } } else { fprintf(stderr, "Not writing pidfile %s: %s\n", path, strerror(errno)); return; } }
- pidを取得して、pidfileのpidを書き込む
pid = getpid(); snprintf(pidstr, sizeof(pidstr), "%d", pid); write(fd, &pidstr, strlen(pidstr)); close(fd);
return; }
呼出元
#related: relatedプラグインは廃止されました。