0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038 #include <errno.h>
0039 #include <fcntl.h>
0040
0041 #include <sys_mqueue.h>
0042 #include <sys/param.h>
0043 #include <sys_mqueue.h>
0044 #include <sys/resource.h>
0045 #include <sys/wait.h>
0046 #include <unistd.h>
0047 #include <fcntl.h>
0048
0049 #include <ndrstandard.h>
0050 #include <ndebug.h>
0051
0052 #include "ndrx.h"
0053 #include <atmi_int.h>
0054 #include <sys_unix.h>
0055 #include <userlog.h>
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069 void sign_chld_handler(int sig)
0070 {
0071 pid_t chldpid;
0072 int stat_loc;
0073 struct rusage rusage;
0074
0075
0076 memset(&rusage, 0, sizeof(rusage));
0077
0078 if (0!=(chldpid = wait3(&stat_loc, WNOHANG|WUNTRACED, &rusage)))
0079 {
0080
0081
0082
0083
0084
0085 G_config.ndrxd_stat=NDRXD_STAT_NOT_STARTED;
0086
0087 }
0088 else
0089 {
0090
0091 }
0092 }
0093
0094
0095
0096
0097
0098
0099 exprivate void ndrx_ndrxd_quick_probe(pid_t pid)
0100 {
0101 int i;
0102
0103
0104 if ((mqd_t)EXFAIL!=G_config.ndrxd_q)
0105 {
0106 goto out;
0107 }
0108
0109
0110 for (i=0; i<6 && EXSUCCEED==kill(pid, 0); i++)
0111 {
0112 usleep(50000*i);
0113
0114 G_config.ndrxd_q = ndrx_mq_open_at_wrp (G_config.ndrxd_q_str, O_WRONLY);
0115
0116
0117 if((mqd_t)EXFAIL!=G_config.ndrxd_q || ENOENT!=errno)
0118 {
0119 break;
0120 }
0121 }
0122
0123 out:
0124 return;
0125 }
0126
0127
0128
0129
0130
0131
0132
0133
0134 expublic int is_ndrxd_running(pid_t *p_pid, int silent)
0135 {
0136 int ret = EXFALSE;
0137 FILE *f = NULL;
0138 pid_t pid;
0139 char pidbuf[64] = {EXEOS};
0140
0141
0142 G_config.ndrxd_stat = NDRXD_STAT_NOT_STARTED;
0143
0144 NDRX_LOG(log_info, "Probing for ndrxd...");
0145
0146
0147 if ((mqd_t)EXFAIL==G_config.ndrxd_q)
0148 G_config.ndrxd_q = ndrx_mq_open_at_wrp (G_config.ndrxd_q_str, O_WRONLY);
0149
0150 if ((mqd_t)EXFAIL==G_config.ndrxd_q)
0151 {
0152 if (ENOENT==errno)
0153 {
0154 NDRX_LOG(log_info, "ndrxd queue [%s] not found - continue probing",
0155 G_config.ndrxd_q_str);
0156 }
0157 else
0158 {
0159 NDRX_LOG(log_error, "Failed to open ndrxd queue [%s]: %s",
0160 G_config.ndrxd_q_str, strerror(errno));
0161 }
0162 }
0163
0164
0165
0166
0167
0168
0169
0170 if (NULL==(f=NDRX_FOPEN(G_config.pid_file, "r")))
0171 {
0172 if (ENOENT==errno)
0173 {
0174 NDRX_LOG(log_info, "ndrxd PID file [%s] not found - continue probing",
0175 G_config.pid_file);
0176 }
0177 else
0178 {
0179 NDRX_LOG(log_error, "Failed to open ndrxd PID file: [%s]: %s",
0180 G_config.pid_file, strerror(errno));
0181 }
0182
0183 goto out;
0184 }
0185
0186
0187 if (NULL==fgets(pidbuf, sizeof(pidbuf), f))
0188 {
0189 NDRX_LOG(log_error, "Failed to read from PID file: [%s]: %s",
0190 G_config.pid_file, strerror(errno));
0191 goto out;
0192 }
0193
0194
0195 fprintf(stderr, "* ndrxd PID (from PID file): %s\n", pidbuf);
0196
0197 NDRX_FCLOSE(f);
0198 f = NULL;
0199
0200 pid = atoi(pidbuf);
0201
0202 if (ndrx_sys_is_process_running(pid, "ndrxd"))
0203 {
0204 if ((mqd_t)EXFAIL!=G_config.ndrxd_q)
0205 {
0206 ret=EXTRUE;
0207 G_config.ndrxd_stat = NDRXD_STAT_RUNNING;
0208 }
0209 else
0210 {
0211
0212 NDRX_LOG(log_error, "`ndrxd' is running, but queue [%s] is "
0213 "not open - malfunction!",
0214 G_config.ndrxd_q_str);
0215 G_config.ndrxd_stat = NDRXD_STAT_MALFUNCTION;
0216 }
0217 }
0218
0219 if (NULL!=p_pid)
0220 {
0221 *p_pid = pid;
0222 }
0223
0224 out:
0225
0226
0227 if (NULL!=f)
0228 NDRX_FCLOSE(f);
0229
0230 if (!ret)
0231 {
0232 if (!silent)
0233 {
0234 fprintf(stderr, "* Enduro/X back-end (ndrxd) is not running\n");
0235 }
0236
0237 if ((mqd_t)EXFAIL!=G_config.ndrxd_q)
0238 {
0239 ndrx_mq_close(G_config.ndrxd_q);
0240 G_config.ndrxd_q = (mqd_t)EXFAIL;
0241
0242 if (ndrx_chk_ndrxd())
0243 {
0244
0245
0246
0247
0248 G_config.ndrxd_stat = NDRXD_STAT_MALFUNCTION;
0249 }
0250 else
0251 {
0252
0253 fprintf(stderr, "ndrxd is not running - system cleanup\n");
0254 ndrx_mq_unlink(G_config.ndrxd_q_str);
0255 }
0256 }
0257 }
0258 else if ((mqd_t)EXFAIL!=G_config.ndrxd_q)
0259 {
0260 ndrx_mq_close(G_config.ndrxd_q);
0261 G_config.ndrxd_q=(mqd_t)EXFAIL;
0262 }
0263
0264 NDRX_LOG(log_info, "%s returns: %s", __func__, (ret?"running":"not running"));
0265
0266 return ret;
0267 }
0268
0269
0270
0271
0272
0273 expublic int start_daemon_idle(void)
0274 {
0275 int ret=EXSUCCEED;
0276 pid_t pid;
0277 char key[NDRX_MAX_KEY_SIZE+3+1];
0278
0279 pid = ndrx_fork();
0280
0281 if( pid == 0)
0282 {
0283 FILE *f;
0284 int fd;
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301 NDRX_LOG(log_debug, "forked close ndrxd_q %p", (void *)(long)G_config.ndrxd_q);
0302
0303 if (G_config.ndrxd_q != (mqd_t)EXFAIL)
0304 ndrx_mq_close(G_config.ndrxd_q);
0305
0306 NDRX_LOG(log_debug, "forked close reply_queue %p",
0307 (void *)(long)G_config.reply_queue);
0308
0309
0310 if (G_config.reply_queue != (mqd_t)EXFAIL)
0311 ndrx_mq_close(G_config.reply_queue);
0312
0313
0314 snprintf(key, sizeof(key), NDRX_KEY_FMT, ndrx_get_G_atmi_env()->rnd_key);
0315 char *cmd[] = { "ndrxd", key, (char *)0 };
0316
0317
0318 if (NULL==(f=NDRX_FOPEN(G_config.ndrxd_logfile, "a")))
0319 {
0320 fprintf(stderr, "Failed to open ndrxd log file: %s\n",
0321 G_config.ndrxd_logfile);
0322 }
0323 else
0324 {
0325
0326
0327 if (EXSUCCEED!=fcntl(fileno(f), F_SETFD, FD_CLOEXEC))
0328 {
0329 userlog("WARNING: Failed to set FD_CLOEXEC: %s", strerror(errno));
0330 }
0331
0332
0333 close(1);
0334 close(2);
0335 if (EXFAIL==dup(fileno(f)))
0336 {
0337 userlog("%s: Failed to dup(1): %s", __func__, strerror(errno));
0338 }
0339
0340 if (EXFAIL==dup(fileno(f)))
0341 {
0342 userlog("%s: Failed to dup(2): %s", __func__, strerror(errno));
0343 }
0344 }
0345
0346
0347 if (EXFAIL!=(fd = open("/dev/null", O_RDWR)))
0348 {
0349 dup2(fd, 0);
0350 close(fd);
0351 }
0352
0353
0354 umask(0);
0355
0356 setsid();
0357
0358 if (EXSUCCEED != execvp ("ndrxd", cmd))
0359 {
0360 fprintf(stderr, "Failed to start server - ndrxd: %s\n",
0361 strerror(errno));
0362 exit(1);
0363 }
0364 }
0365 else
0366 {
0367 int i;
0368 int started=EXFALSE;
0369
0370
0371
0372 ndrx_ndrxd_quick_probe(pid);
0373 started=is_ndrxd_running(NULL, EXTRUE);
0374
0375 #define MAX_WSLEEP 5
0376
0377 if (!started)
0378 {
0379 for (i=0; i<MAX_WSLEEP; i++)
0380 {
0381 fprintf(stderr, "* Still not started, waiting %d/%d\n",
0382 i+2, MAX_WSLEEP+1);
0383 sleep(1);
0384 started=is_ndrxd_running(NULL, EXTRUE);
0385 if (started)
0386 break;
0387 }
0388 }
0389
0390 if (started)
0391 {
0392 fprintf(stderr, "* ndrxd idle instance started.\n");
0393 G_config.is_idle = EXTRUE;
0394 }
0395 else if (NDRXD_STAT_NOT_STARTED==G_config.ndrxd_stat)
0396 {
0397 fprintf(stderr, "* ndrxd idle instance not started (something failed?)!\n");
0398 }
0399 else
0400 {
0401 fprintf(stderr, "* ndrxd instance idle malfunction!\n");
0402 }
0403 }
0404
0405 return ret;
0406 }
0407
0408