1 /*
2  * src/test/examples/testlibpq2.c
3  *
4  *
5  * testlibpq2.c
6  *		Test of the asynchronous notification interface
7  *
8  * Start this program, then from psql in another window do
9  *	 NOTIFY TBL2;
10  * Repeat four times to get this program to exit.
11  *
12  * Or, if you want to get fancy, try this:
13  * populate a database with the following commands
14  * (provided in src/test/examples/testlibpq2.sql):
15  *
16  *	 CREATE SCHEMA TESTLIBPQ2;
17  *	 SET search_path = TESTLIBPQ2;
18  *	 CREATE TABLE TBL1 (i int4);
19  *	 CREATE TABLE TBL2 (i int4);
20  *	 CREATE RULE r1 AS ON INSERT TO TBL1 DO
21  *	   (INSERT INTO TBL2 VALUES (new.i); NOTIFY TBL2);
22  *
23  * Start this program, then from psql do this four times:
24  *
25  *	 INSERT INTO TESTLIBPQ2.TBL1 VALUES (10);
26  */
27 
28 #ifdef WIN32
29 #include <windows.h>
30 #endif
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_SELECT_H
38 #include <sys/select.h>
39 #endif
40 
41 #include "libpq-fe.h"
42 
43 static void
exit_nicely(PGconn * conn)44 exit_nicely(PGconn *conn)
45 {
46 	PQfinish(conn);
47 	exit(1);
48 }
49 
50 int
main(int argc,char ** argv)51 main(int argc, char **argv)
52 {
53 	const char *conninfo;
54 	PGconn	   *conn;
55 	PGresult   *res;
56 	PGnotify   *notify;
57 	int			nnotifies;
58 
59 	/*
60 	 * If the user supplies a parameter on the command line, use it as the
61 	 * conninfo string; otherwise default to setting dbname=postgres and using
62 	 * environment variables or defaults for all other connection parameters.
63 	 */
64 	if (argc > 1)
65 		conninfo = argv[1];
66 	else
67 		conninfo = "dbname = postgres";
68 
69 	/* Make a connection to the database */
70 	conn = PQconnectdb(conninfo);
71 
72 	/* Check to see that the backend connection was successfully made */
73 	if (PQstatus(conn) != CONNECTION_OK)
74 	{
75 		fprintf(stderr, "%s", PQerrorMessage(conn));
76 		exit_nicely(conn);
77 	}
78 
79 	/* Set always-secure search path, so malicious users can't take control. */
80 	res = PQexec(conn,
81 				 "SELECT pg_catalog.set_config('search_path', '', false)");
82 	if (PQresultStatus(res) != PGRES_TUPLES_OK)
83 	{
84 		fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
85 		PQclear(res);
86 		exit_nicely(conn);
87 	}
88 
89 	/*
90 	 * Should PQclear PGresult whenever it is no longer needed to avoid memory
91 	 * leaks
92 	 */
93 	PQclear(res);
94 
95 	/*
96 	 * Issue LISTEN command to enable notifications from the rule's NOTIFY.
97 	 */
98 	res = PQexec(conn, "LISTEN TBL2");
99 	if (PQresultStatus(res) != PGRES_COMMAND_OK)
100 	{
101 		fprintf(stderr, "LISTEN command failed: %s", PQerrorMessage(conn));
102 		PQclear(res);
103 		exit_nicely(conn);
104 	}
105 	PQclear(res);
106 
107 	/* Quit after four notifies are received. */
108 	nnotifies = 0;
109 	while (nnotifies < 4)
110 	{
111 		/*
112 		 * Sleep until something happens on the connection.  We use select(2)
113 		 * to wait for input, but you could also use poll() or similar
114 		 * facilities.
115 		 */
116 		int			sock;
117 		fd_set		input_mask;
118 
119 		sock = PQsocket(conn);
120 
121 		if (sock < 0)
122 			break;				/* shouldn't happen */
123 
124 		FD_ZERO(&input_mask);
125 		FD_SET(sock, &input_mask);
126 
127 		if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0)
128 		{
129 			fprintf(stderr, "select() failed: %s\n", strerror(errno));
130 			exit_nicely(conn);
131 		}
132 
133 		/* Now check for input */
134 		PQconsumeInput(conn);
135 		while ((notify = PQnotifies(conn)) != NULL)
136 		{
137 			fprintf(stderr,
138 					"ASYNC NOTIFY of '%s' received from backend PID %d\n",
139 					notify->relname, notify->be_pid);
140 			PQfreemem(notify);
141 			nnotifies++;
142 			PQconsumeInput(conn);
143 		}
144 	}
145 
146 	fprintf(stderr, "Done.\n");
147 
148 	/* close the connection to the database and cleanup */
149 	PQfinish(conn);
150 
151 	return 0;
152 }
153