]> git.8kb.co.uk Git - pgpool-ii/pgpool-ii_2.2.5/blob - pcp/pcp_node_info.c
Attempt to send a proper failure message to frontend when authentication
[pgpool-ii/pgpool-ii_2.2.5] / pcp / pcp_node_info.c
1 /*
2  * $Header: /cvsroot/pgpool/pgpool-II/pcp/pcp_node_info.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 info" 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 nodeID;
42         BackendInfo *backend_info;
43         int ch;
44
45         while ((ch = getopt(argc, argv, "hd")) != -1) {
46                 switch (ch) {
47                 case 'd':
48                         pcp_enable_debug();
49                         break;
50
51                 case 'h':
52                 case '?':
53                 default:
54                         usage();
55                         exit(0);
56                 }
57         }
58         argc -= optind;
59         argv += optind;
60
61         if (argc != 6)
62         {
63                 errorcode = INVALERR;
64                 pcp_errorstr(errorcode);
65                 myexit(errorcode);
66         }
67
68         timeout = atol(argv[0]);
69         if (timeout < 0) {
70                 errorcode = INVALERR;
71                 pcp_errorstr(errorcode);
72                 myexit(errorcode);
73         }
74
75         if (strlen(argv[1]) >= MAX_DB_HOST_NAMELEN)
76         {
77                 errorcode = INVALERR;
78                 pcp_errorstr(errorcode);
79                 myexit(errorcode);
80         }
81         strcpy(host, argv[1]);
82
83         port = atoi(argv[2]);
84         if (port <= 1024 || port > 65535)
85         {
86                 errorcode = INVALERR;
87                 pcp_errorstr(errorcode);
88                 myexit(errorcode);
89         }
90
91         if (strlen(argv[3]) >= MAX_USER_PASSWD_LEN)
92         {
93                 errorcode = INVALERR;
94                 pcp_errorstr(errorcode);
95                 myexit(errorcode);
96         }
97         strcpy(user, argv[3]);
98
99         if (strlen(argv[4]) >= MAX_USER_PASSWD_LEN)
100         {
101                 errorcode = INVALERR;
102                 pcp_errorstr(errorcode);
103                 myexit(errorcode);
104         }
105         strcpy(pass, argv[4]);
106
107         nodeID = atoi(argv[5]);
108         if (nodeID < 0 || nodeID > MAX_NUM_BACKENDS)
109         {
110                 errorcode = INVALERR;
111                 pcp_errorstr(errorcode);
112                 myexit(errorcode);
113         }
114
115         pcp_set_timeout(timeout);
116
117         if (pcp_connect(host, port, user, pass))
118         {
119                 pcp_errorstr(errorcode);
120                 myexit(errorcode);
121         }
122
123         if ((backend_info = pcp_node_info(nodeID)) == NULL)
124         {
125                 pcp_errorstr(errorcode);
126                 pcp_disconnect();
127                 myexit(errorcode);
128         } else {
129                 printf("%s %d %d %f\n", 
130                            backend_info->backend_hostname,
131                            backend_info->backend_port,
132                            backend_info->backend_status,
133                            backend_info->backend_weight);
134
135                 free(backend_info);
136         }
137
138         pcp_disconnect();
139
140         return 0;
141 }
142
143 static void
144 usage(void)
145 {
146         fprintf(stderr, "pcp_node_info - display a pgpool-II node's information\n\n");
147         fprintf(stderr, "Usage: pcp_node_info [-d] timeout hostname port# username password nodeID\n");
148         fprintf(stderr, "Usage: pcp_node_info -h\n\n");
149         fprintf(stderr, "  -d       - enable debug message (optional)\n");
150         fprintf(stderr, "  timeout  - connection timeout value in seconds. command exits on timeout\n");
151         fprintf(stderr, "  hostname - pgpool-II hostname\n");
152         fprintf(stderr, "  port#    - pgpool-II port number\n");
153         fprintf(stderr, "  username - username for PCP authentication\n");
154         fprintf(stderr, "  password - password for PCP authentication\n");
155         fprintf(stderr, "  nodeID   - ID of a node to get information for\n");
156         fprintf(stderr, "  -h       - print this help\n");
157 }
158
159 static void
160 myexit(ErrorCode e)
161 {
162         if (e == INVALERR)
163         {
164                 usage();
165                 exit(e);
166         }
167
168         exit(e);
169 }