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