]> git.8kb.co.uk Git - pgpool-ii/pgpool-ii_2.2.5/blob - pcp/pcp_proc_count.c
Attempt to send a proper failure message to frontend when authentication
[pgpool-ii/pgpool-ii_2.2.5] / pcp / pcp_proc_count.c
1 /*
2  * $Header: /cvsroot/pgpool/pgpool-II/pcp/pcp_proc_count.c,v 1.4 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 "process 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 process_count;
42         int *process_list = NULL;
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 != 5) {
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                 errorcode = INVALERR;
76                 pcp_errorstr(errorcode);
77                 myexit(errorcode);
78         }
79         strcpy(host, argv[1]);
80
81         port = atoi(argv[2]);
82         if (port <= 1024 || port > 65535) {
83                 errorcode = INVALERR;
84                 pcp_errorstr(errorcode);
85                 myexit(errorcode);
86         }
87
88         if (strlen(argv[3]) >= MAX_USER_PASSWD_LEN) {
89                 errorcode = INVALERR;
90                 pcp_errorstr(errorcode);
91                 myexit(errorcode);
92         }
93         strcpy(user, argv[3]);
94
95         if (strlen(argv[4]) >= MAX_USER_PASSWD_LEN) {
96                 errorcode = INVALERR;
97                 pcp_errorstr(errorcode);
98                 myexit(errorcode);
99         }
100         strcpy(pass, argv[4]);
101
102         pcp_set_timeout(timeout);
103
104         if (pcp_connect(host, port, user, pass))
105         {
106                 pcp_errorstr(errorcode);
107                 myexit(errorcode);
108         }
109
110         if ((process_list = pcp_process_count(&process_count)) == NULL)
111         {
112                 pcp_errorstr(errorcode);
113                 pcp_disconnect();
114                 myexit(errorcode);
115         } else {
116                 int i;
117                 
118                 for (i = 0; i < process_count; i++)
119                         printf("%d ", process_list[i]);
120                 printf("\n");
121
122                 free(process_list);
123         }
124
125         pcp_disconnect();
126
127         return 0;
128 }
129
130 static void
131 usage(void)
132 {
133         fprintf(stderr, "pcp_proc_count - display the list of pgpool-II child process PIDs\n\n");
134         fprintf(stderr, "Usage: pcp_proc_count timeout hostname port# username password\n");
135         fprintf(stderr, "Usage: pcp_node_info -h\n\n");
136         fprintf(stderr, "  timeout  - connection timeout value in seconds. command exits on timeout\n");
137         fprintf(stderr, "  hostname - pgpool-II hostname\n");
138         fprintf(stderr, "  port#    - pgpool-II port number\n");
139         fprintf(stderr, "  username - username for PCP authentication\n");
140         fprintf(stderr, "  password - password for PCP authentication\n");
141         fprintf(stderr, "  -h       - print this help\n");
142 }
143
144 static void
145 myexit(ErrorCode e)
146 {
147         if (e == INVALERR)
148         {
149                 usage();
150                 exit(e);
151         }
152
153         exit(e);
154 }