]> git.8kb.co.uk Git - pgpool-ii/pgpool-ii_2.2.5/blob - pcp/pcp_node_count.c
Attempt to send a proper failure message to frontend when authentication
[pgpool-ii/pgpool-ii_2.2.5] / pcp / pcp_node_count.c
1 /*
2  * $Header: /cvsroot/pgpool/pgpool-II/pcp/pcp_node_count.c,v 1.3 2008/12/31 10:25:40 t-ishii Exp $
3  *
4  * pgpool: a language independent connection pool server for PostgreSQL 
5  * written by Tatsuo Ishii
6  *
7  * Copyright (c) 2003-2008      PgPool Global Development Group
8  *
9  * Permission to use, copy, modify, and distribute this software and
10  * its documentation for any purpose and without fee is hereby
11  * granted, provided that the above copyright notice appear in all
12  * copies and that both that copyright notice and this permission
13  * notice appear in supporting documentation, and that the name of the
14  * author not be used in advertising or publicity pertaining to
15  * distribution of the software without specific, written prior
16  * permission. The author makes no representations about the
17  * suitability of this software for any purpose.  It is provided "as
18  * is" without express or implied warranty.
19  *
20  * Client program to send "node count" command.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "pcp.h"
29
30 static void usage(void);
31 static void myexit(ErrorCode e);
32
33 int
34 main(int argc, char **argv)
35 {
36         long timeout;
37         char host[MAX_DB_HOST_NAMELEN];
38         int port;
39         char user[MAX_USER_PASSWD_LEN];
40         char pass[MAX_USER_PASSWD_LEN];
41         int node_count;
42         int ch;
43
44         while ((ch = getopt(argc, argv, "hd")) != -1) {
45                 switch (ch) {
46                 case 'd':
47                         pcp_enable_debug();
48                         break;
49
50                 case 'h':
51                 case '?':
52                 default:
53                         usage();
54                         exit(0);
55                 }
56         }
57         argc -= optind;
58         argv += optind;
59
60         if (argc != 5) {
61                 errorcode = INVALERR;
62                 pcp_errorstr(errorcode);
63                 myexit(errorcode);
64         }
65
66         timeout = atol(argv[0]);
67         if (timeout < 0) {
68                 errorcode = INVALERR;
69                 pcp_errorstr(errorcode);
70                 myexit(errorcode);
71         }
72
73         if (strlen(argv[1]) >= MAX_DB_HOST_NAMELEN) {
74                 errorcode = INVALERR;
75                 pcp_errorstr(errorcode);
76                 myexit(errorcode);
77         }
78         strcpy(host, argv[1]);
79
80         port = atoi(argv[2]);
81         if (port <= 1024 || port > 65535) {
82                 errorcode = INVALERR;
83                 pcp_errorstr(errorcode);
84                 myexit(errorcode);
85         }
86
87         if (strlen(argv[3]) >= MAX_USER_PASSWD_LEN) {
88                 errorcode = INVALERR;
89                 pcp_errorstr(errorcode);
90                 myexit(errorcode);
91         }
92         strcpy(user, argv[3]);
93
94         if (strlen(argv[4]) >= MAX_USER_PASSWD_LEN) {
95                 errorcode = INVALERR;
96                 pcp_errorstr(errorcode);
97                 myexit(errorcode);
98         }
99         strcpy(pass, argv[4]);
100
101         pcp_set_timeout(timeout);
102
103         if (pcp_connect(host, port, user, pass))
104         {
105                 pcp_errorstr(errorcode);
106                 myexit(errorcode);
107         }
108
109         if ((node_count = pcp_node_count()) < 0)
110         {
111                 pcp_errorstr(errorcode);
112                 pcp_disconnect();
113                 myexit(errorcode);
114         } else {
115                 printf("%d\n", node_count);
116         }
117
118         pcp_disconnect();
119
120         return 0;
121 }
122
123 static void
124 usage(void)
125 {
126         fprintf(stderr, "pcp_node_count - display the total number of nodes under pgpool-II's control\n\n");
127         fprintf(stderr, "Usage: pcp_node_count [-d] timeout hostname port# username password\n");
128         fprintf(stderr, "Usage: pcp_node_count -h\n\n");
129         fprintf(stderr, "  -d       - enable debug message (optional)\n");
130         fprintf(stderr, "  timeout  - connection timeout value in seconds. command exits on timeout\n");
131         fprintf(stderr, "  hostname - pgpool-II hostname\n");
132         fprintf(stderr, "  port#    - pgpool-II port number\n");
133         fprintf(stderr, "  username - username for PCP authentication\n");
134         fprintf(stderr, "  password - password for PCP authentication\n");
135         fprintf(stderr, "  -h       - print this help\n");
136 }
137
138 static void
139 myexit(ErrorCode e)
140 {
141         if (e == INVALERR)
142         {
143                 usage();
144                 exit(e);
145         }
146
147         exit(e);
148 }