/* * $Header$ * * pgpool: a language independent connection pool server for PostgreSQL * written by Tatsuo Ishii * * Copyright (c) 2003-2013 PgPool Global Development Group * * Permission to use, copy, modify, and distribute this software and * its documentation for any purpose and without fee is hereby * granted, provided that the above copyright notice appear in all * copies and that both that copyright notice and this permission * notice appear in supporting documentation, and that the name of the * author not be used in advertising or publicity pertaining to * distribution of the software without specific, written prior * permission. The author makes no representations about the * suitability of this software for any purpose. It is provided "as * is" without express or implied warranty. * * Client program to send "stop pgpool" command. */ #include #include #include #include #ifdef HAVE_GETOPT_H #include #else #include "getopt_long.h" #endif #include "pcp.h" static void usage(void); static void myexit(ErrorCode e); int main(int argc, char **argv) { long timeout; char host[MAX_DB_HOST_NAMELEN]; int port; char user[MAX_USER_PASSWD_LEN]; char pass[MAX_USER_PASSWD_LEN]; char mode; int ch; int optindex; static struct option long_options[] = { {"debug", no_argument, NULL, 'd'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0} }; while ((ch = getopt_long(argc, argv, "hd", long_options, &optindex)) != -1) { switch (ch) { case 'd': pcp_enable_debug(); break; case 'h': case '?': default: usage(); exit(0); } } argc -= optind; argv += optind; if (argc != 6) { errorcode = INVALERR; pcp_errorstr(errorcode); myexit(errorcode); } timeout = atol(argv[0]); if (timeout < 0) { errorcode = INVALERR; pcp_errorstr(errorcode); myexit(errorcode); } if (strlen(argv[1]) >= MAX_DB_HOST_NAMELEN) { errorcode = INVALERR; pcp_errorstr(errorcode); myexit(errorcode); } strcpy(host, argv[1]); port = atoi(argv[2]); if (port <= 1024 || port > 65535) { errorcode = INVALERR; pcp_errorstr(errorcode); myexit(errorcode); } if (strlen(argv[3]) >= MAX_USER_PASSWD_LEN) { errorcode = INVALERR; pcp_errorstr(errorcode); myexit(errorcode); } strcpy(user, argv[3]); if (strlen(argv[4]) >= MAX_USER_PASSWD_LEN) { errorcode = INVALERR; pcp_errorstr(errorcode); myexit(errorcode); } strcpy(pass, argv[4]); if (strlen(argv[5]) != 1) { errorcode = INVALERR; pcp_errorstr(errorcode); myexit(errorcode); } mode = argv[5][0]; if (mode != 's' && mode != 'f' && mode != 'i') { errorcode = INVALERR; pcp_errorstr(errorcode); myexit(errorcode); } pcp_set_timeout(timeout); if (pcp_connect(host, port, user, pass)) { pcp_errorstr(errorcode); myexit(errorcode); } if (pcp_terminate_pgpool(mode)) { pcp_errorstr(errorcode); pcp_disconnect(); myexit(errorcode); } pcp_disconnect(); return 0; } static void usage(void) { fprintf(stderr, "pcp_stop_pgpool - terminate pgpool-II\n\n"); fprintf(stderr, "Usage: pcp_stop_pgpool [-d] timeout hostname port# username password mode\n"); fprintf(stderr, "Usage: pcp_stop_pgpool -h\n\n"); fprintf(stderr, " -d, --debug : enable debug message (optional)\n"); fprintf(stderr, " timeout : connection timeout value in seconds. command exits on timeout\n"); fprintf(stderr, " hostname : pgpool-II hostname\n"); fprintf(stderr, " port# : PCP port number\n"); fprintf(stderr, " username : username for PCP authentication\n"); fprintf(stderr, " password : password for PCP authentication\n"); fprintf(stderr, " mode : shutdown mode\n"); fprintf(stderr, " s - smart shutdown f - fast shutdown i - immediate shutdown\n"); fprintf(stderr, " -h, --help : print this help\n"); } static void myexit(ErrorCode e) { if (e == INVALERR) { usage(); exit(e); } exit(e); }