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, "Connection to database failed: %s",
76 				PQerrorMessage(conn));
77 		exit_nicely(conn);
78 	}
79 
80 	/* Set always-secure search path, so malicous users can't take control. */
81 	res = PQexec(conn,
82 				 "SELECT pg_catalog.set_config('search_path', '', false)");
83 	if (PQresultStatus(res) != PGRES_TUPLES_OK)
84 	{
85 		fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
86 		PQclear(res);
87 		exit_nicely(conn);
88 	}
89 
90 	/*
91 	 * Should PQclear PGresult whenever it is no longer needed to avoid memory
92 	 * leaks
93 	 */
94 	PQclear(res);
95 
96 	/*
97 	 * Issue LISTEN command to enable notifications from the rule's NOTIFY.
98 	 */
99 	res = PQexec(conn, "LISTEN TBL2");
100 	if (PQresultStatus(res) != PGRES_COMMAND_OK)
101 	{
102 		fprintf(stderr, "LISTEN command failed: %s", PQerrorMessage(conn));
103 		PQclear(res);
104 		exit_nicely(conn);
105 	}
106 	PQclear(res);
107 
108 	/* Quit after four notifies are received. */
109 	nnotifies = 0;
110 	while (nnotifies < 4)
111 	{
112 		/*
113 		 * Sleep until something happens on the connection.  We use select(2)
114 		 * to wait for input, but you could also use poll() or similar
115 		 * facilities.
116 		 */
117 		int			sock;
118 		fd_set		input_mask;
119 
120 		sock = PQsocket(conn);
121 
122 		if (sock < 0)
123 			break;				/* shouldn't happen */
124 
125 		FD_ZERO(&input_mask);
126 		FD_SET(sock, &input_mask);
127 
128 		if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0)
129 		{
130 			fprintf(stderr, "select() failed: %s\n", strerror(errno));
131 			exit_nicely(conn);
132 		}
133 
134 		/* Now check for input */
135 		PQconsumeInput(conn);
136 		while ((notify = PQnotifies(conn)) != NULL)
137 		{
138 			fprintf(stderr,
139 					"ASYNC NOTIFY of '%s' received from backend PID %d\n",
140 					notify->relname, notify->be_pid);
141 			PQfreemem(notify);
142 			nnotifies++;
143 			PQconsumeInput(conn);
144 		}
145 	}
146 
147 	fprintf(stderr, "Done.\n");
148 
149 	/* close the connection to the database and cleanup */
150 	PQfinish(conn);
151 
152 	return 0;
153 }
154