]> git.8kb.co.uk Git - pgpool-ii/pgpool-ii_2.2.5/blob - pcp/pcp_detach_node.c
Attempt to send a proper failure message to frontend when authentication
[pgpool-ii/pgpool-ii_2.2.5] / pcp / pcp_detach_node.c
1 /*
2  * $Header: /cvsroot/pgpool/pgpool-II/pcp/pcp_detach_node.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 "detach node" 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         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 != 6)
61         {
62                 errorcode = INVALERR;
63                 pcp_errorstr(errorcode);
64                 myexit(errorcode);
65         }
66
67         timeout = atol(argv[0]);
68         if (timeout < 0) {
69                 errorcode = INVALERR;
70                 pcp_errorstr(errorcode);
71                 myexit(errorcode);
72         }
73
74         if (strlen(argv[1]) >= MAX_DB_HOST_NAMELEN)
75         {
76                 errorcode = INVALERR;
77                 pcp_errorstr(errorcode);
78                 myexit(errorcode);
79         }
80         strcpy(host, argv[1]);
81
82         port = atoi(argv[2]);
83         if (port <= 1024 || port > 65535)
84         {
85                 errorcode = INVALERR;
86                 pcp_errorstr(errorcode);
87                 myexit(errorcode);
88         }
89
90         if (strlen(argv[3]) >= MAX_USER_PASSWD_LEN)
91         {
92                 errorcode = INVALERR;
93                 pcp_errorstr(errorcode);
94                 myexit(errorcode);
95         }
96         strcpy(user, argv[3]);
97
98         if (strlen(argv[4]) >= MAX_USER_PASSWD_LEN)
99         {
100                 errorcode = INVALERR;
101                 pcp_errorstr(errorcode);
102                 myexit(errorcode);
103         }
104         strcpy(pass, argv[4]);
105
106         nodeID = atoi(argv[5]);
107         if (nodeID < 0 || nodeID > MAX_NUM_BACKENDS)
108         {
109                 errorcode = INVALERR;
110                 pcp_errorstr(errorcode);
111                 myexit(errorcode);
112         }
113                 
114         pcp_set_timeout(timeout);
115
116         if (pcp_connect(host, port, user, pass))
117         {
118                 pcp_errorstr(errorcode);
119                 myexit(errorcode);
120         }
121
122         if (pcp_detach_node(nodeID))
123         {
124                 pcp_errorstr(errorcode);
125                 pcp_disconnect();
126                 myexit(errorcode);
127         }
128
129         pcp_disconnect();
130
131         return 0;
132 }
133
134 static void
135 usage(void)
136 {
137         fprintf(stderr, "pcp_detach_node - detach a node from pgpool-II\n\n");
138         fprintf(stderr, "Usage: pcp_detach_node [-d] timeout hostname port# username password nodeID\n");
139         fprintf(stderr, "Usage: pcp_detach_node -h\n\n");
140         fprintf(stderr, "  -d       - enable debug message (optional)\n");
141         fprintf(stderr, "  timeout  - connection timeout value in seconds. command exits on timeout\n");
142         fprintf(stderr, "  hostname - pgpool-II hostname\n");
143         fprintf(stderr, "  port#    - pgpool-II port number\n");
144         fprintf(stderr, "  username - username for PCP authentication\n");
145         fprintf(stderr, "  password - password for PCP authentication\n");
146         fprintf(stderr, "  nodeID   - ID of a node to be detached\n");
147         fprintf(stderr, "  -h       - print this help\n");
148 }
149
150 static void
151 myexit(ErrorCode e)
152 {
153         if (e == INVALERR)
154         {
155                 usage();
156                 exit(e);
157         }
158
159         exit(e);
160 }