write MaxScale PID into pidfile

write MaxScale PID into pidfile
This commit is contained in:
MassimilianoPinto
2014-06-29 17:53:25 +02:00
parent 3b14c780ad
commit ee582bf2b2

View File

@ -35,6 +35,7 @@
* calls to log manager and to query classifier.
* Put example code behind SS_DEBUG macros.
* 05/02/14 Mark Riddoch Addition of version string
* 29/06/14 Massimiliano Pinto Addition of pidfile
*
* @endverbatim
*/
@ -109,6 +110,9 @@ static char* server_groups[] = {
/* The data directory we created for this gateway instance */
static char datadir[PATH_MAX+1] = "";
/* The data directory we created for this gateway instance */
static char pidfile[PATH_MAX+1] = "";
/**
* exit flag for log flusher.
*/
@ -126,6 +130,8 @@ static bool daemon_mode = true;
static void log_flush_shutdown(void);
static void log_flush_cb(void* arg);
static int write_pid_file(char *); /* write MaxScale pidfile */
static void unlink_pidfile(void); /* remove pidfile */
static void libmysqld_done(void);
static bool file_write_header(FILE* outfile);
static bool file_write_footer(FILE* outfile);
@ -1018,6 +1024,7 @@ int main(int argc, char **argv)
goto return_main;
}
}
if (!daemon_mode)
{
fprintf(stderr,
@ -1137,6 +1144,8 @@ int main(int argc, char **argv)
rc = MAXSCALE_INTERNALERROR;
goto return_main;
}
/* register exit function for embedded MySQL library */
l = atexit(libmysqld_done);
if (l != 0) {
@ -1148,6 +1157,7 @@ int main(int argc, char **argv)
rc = MAXSCALE_INTERNALERROR;
goto return_main;
}
/*<
* If MaxScale home directory wasn't set by command-line argument.
* Next, resolve it from environment variable and further on,
@ -1323,6 +1333,10 @@ int main(int argc, char **argv)
"MaxScale is running in process %i",
getpid())));
/* Write process pid into MaxScale pidfile */
write_pid_file(home_dir);
/* Init MaxScale poll system */
poll_init();
/*<
@ -1363,6 +1377,7 @@ int main(int argc, char **argv)
* Serve clients.
*/
poll_waitevents((void *)0);
/*<
* Wait server threads' completion.
*/
@ -1389,6 +1404,9 @@ int main(int argc, char **argv)
LOGFILE_MESSAGE,
"MaxScale shutdown completed.")));
/* Remove Pidfile */
unlink_pidfile();
return_main:
return rc;
} /*< End of main */
@ -1435,3 +1453,59 @@ static void log_flush_cb(
LOGIF(LM, (skygw_log_write(LOGFILE_MESSAGE,
"Finished MaxScale log flusher.")));
}
/**
* Unlink pid file, called at program exit
*/
static void unlink_pidfile(void)
{
if (strlen(pidfile)) {
if (unlink(pidfile)) {
fprintf(stderr, "MaxScale failed to remove pidfile %s: error %d, %s\n", pidfile, errno, strerror(errno));
}
}
}
/**
* Write process pid into pidfile anc close it
* Parameters:
* @param home_dir The MaxScale home dir
* @return 0 on success, 1 on failure
*
*/
static int write_pid_file(char *home_dir) {
int fd = -1;
snprintf(pidfile, PATH_MAX, "%s/log/maxscale.pid", home_dir);
fd = open(pidfile, O_WRONLY | O_CREAT | O_TRUNC, 0777);
if (fd == -1) {
fprintf(stderr, "MaxScale failed to open pidFile %s: error %d, %s\n", pidfile, errno, strerror(errno));
return 1;
} else {
char pidstr[50]="";
/* truncate pidfile content */
if (ftruncate(fd, 0) == -1) {
fprintf(stderr, "MaxScale failed to truncate pidfile %s: error %d, %s\n", pidfile, errno, strerror(errno));
}
snprintf(pidstr, sizeof(pidstr)-1, "%d", getpid());
if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
fprintf(stderr, "MaxScale failed to write into pidfile %s: error %d, %s\n", pidfile, errno, strerror(errno));
/* close file and return */
close(fd);
return 1;
}
/* close file */
close(fd);
fprintf(stderr, "MaxScale PID %s in pidfile %s\n", pidstr, pidfile);
}
/* success */
return 0;
}