improve rmps shutdown, cleanup main()
This commit is contained in:
43
src/main.c
43
src/main.c
@@ -19,12 +19,7 @@
|
|||||||
* along with RMPS. If not, see <http://www.gnu.org/licenses/>.
|
* along with RMPS. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <signal.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include "confparser.h"
|
#include "confparser.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
@@ -131,46 +126,12 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
|
|
||||||
if (task == STOP || task == RESTART) {
|
if (task == STOP || task == RESTART) {
|
||||||
char buf[10];
|
if (rmps_die() != 0) {
|
||||||
int pid;
|
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
if (task == STOP)
|
|
||||||
log(VERBOSE, "We got a stop signal!");
|
|
||||||
else /* RESTART */
|
|
||||||
log(VERBOSE, "We got a restart signal!");
|
|
||||||
|
|
||||||
fp = fopen(conf.rmps.pidfile, "r");
|
|
||||||
if (fp) {
|
|
||||||
if (!fgets(buf, 10, fp)) {
|
|
||||||
log(ERROR, "Failed to read %s!",
|
|
||||||
conf.rmps.pidfile);
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
pid = strtol(buf, NULL, 10);
|
|
||||||
log(VERBOSE, "Killing RMPS pid - %d", pid);
|
|
||||||
kill(pid, SIGTERM);
|
|
||||||
//waitpid(TODO);
|
|
||||||
} else {
|
|
||||||
switch (errno) {
|
|
||||||
case EACCES:
|
|
||||||
log(ERROR, "Permission denied to read PID. Exiting!");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
case ENOENT:
|
|
||||||
log(VERBOSE, "PID file %s does not exist. Nothing to kill...",
|
|
||||||
conf.rmps.pidfile);
|
|
||||||
if (task == STOP)
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
log(ERROR, "Failed to open PID file %s (errno: %d). Exiting!",
|
|
||||||
conf.rmps.pidfile, errno);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (task == START || task == RESTART)
|
if (task == START || task == RESTART)
|
||||||
launch_rmps(&conf, fork_flag);
|
rmps_launch(&conf, fork_flag);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
44
src/rmps.c
44
src/rmps.c
@@ -30,6 +30,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
/* included for openssl */
|
/* included for openssl */
|
||||||
#include <openssl/ssl.h>
|
#include <openssl/ssl.h>
|
||||||
@@ -286,7 +287,47 @@ void load_certificates(SSL_CTX *ctx, const char *certfile,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void launch_rmps(struct conf_table *conf, int fork_flag)
|
int rmps_die(void)
|
||||||
|
{
|
||||||
|
char buf[10];
|
||||||
|
int pid, status;
|
||||||
|
FILE *fp;
|
||||||
|
|
||||||
|
fp = fopen(conf.rmps.pidfile, "r");
|
||||||
|
if (fp) {
|
||||||
|
if (!fgets(buf, 10, fp)) {
|
||||||
|
log(ERROR, "Failed to read %s!", conf.rmps.pidfile);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
pid = strtol(buf, NULL, 10);
|
||||||
|
kill(pid, SIGTERM);
|
||||||
|
//waitpid(TODO);
|
||||||
|
} else {
|
||||||
|
switch (errno) {
|
||||||
|
case EACCES:
|
||||||
|
log(ERROR, "Permission denied to read PID file %s. Exiting!",
|
||||||
|
conf.rmps.pidfile);
|
||||||
|
return 1;
|
||||||
|
case ENOENT:
|
||||||
|
log(VERBOSE, "PID file %s does not exist. Nothing to kill...",
|
||||||
|
conf.rmps.pidfile);
|
||||||
|
return 1;
|
||||||
|
default:
|
||||||
|
log(ERROR, "Failed to open PID file %s (errno: %d). Exiting!",
|
||||||
|
conf.rmps.pidfile, errno);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
status = kill(pid, SIGTERM);
|
||||||
|
if (status != 0) {
|
||||||
|
log(ERROR, "Failed to kill RMPS PID %s (errno %s). Exiting!");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void rmps_launch(struct conf_table *conf, int fork_flag)
|
||||||
{
|
{
|
||||||
pthread_t pool[2];
|
pthread_t pool[2];
|
||||||
struct pool_data pool_args[2];
|
struct pool_data pool_args[2];
|
||||||
@@ -337,4 +378,3 @@ void launch_rmps(struct conf_table *conf, int fork_flag)
|
|||||||
pthread_join(pool[0], NULL);
|
pthread_join(pool[0], NULL);
|
||||||
pthread_join(pool[1], NULL);
|
pthread_join(pool[1], NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ struct pool_data {
|
|||||||
int size;
|
int size;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern void launch_rmps(struct conf_table *conf, int fork_flag);
|
extern void rmps_launch(struct conf_table *conf, int fork_flag);
|
||||||
|
extern int rmps_die(void);
|
||||||
|
|
||||||
#endif /* RMPS_H */
|
#endif /* RMPS_H */
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user