Disallow empty passwords in LDAP authentication, the same way
[PostgreSQL.git] / src / backend / port / ipc_test.c
blob7c6a0b4eda6498f861fdb7e297e7b4177a7e99be
1 /*-------------------------------------------------------------------------
3 * ipc_test.c
4 * Simplistic testbed for shared memory and semaphore code.
6 * This file allows for quick "smoke testing" of a PG semaphore or shared
7 * memory implementation, with less overhead than compiling up a whole
8 * installation. To use:
9 * 1. Run configure, then edit src/include/pg_config.h to select the
10 * USE_xxx_SEMAPHORES and USE_xxx_SHARED_MEMORY settings you want.
11 * Also, adjust the pg_sema.c and pg_shmem.c symlinks in
12 * src/backend/port/ if needed.
13 * 2. In src/backend/port/, do "gmake ipc_test".
14 * 3. Run ipc_test and see if it works.
15 * 4. If it seems to work, try building the whole system and running
16 * the parallel regression tests for a more complete test.
19 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
20 * Portions Copyright (c) 1994, Regents of the University of California
23 * IDENTIFICATION
24 * $PostgreSQL$
26 *-------------------------------------------------------------------------
28 #include "postgres.h"
30 #include <unistd.h>
32 #include "miscadmin.h"
33 #include "storage/ipc.h"
34 #include "storage/pg_sema.h"
35 #include "storage/pg_shmem.h"
38 /********* stuff needed to satisfy references in shmem/sema code *********/
41 volatile bool InterruptPending = false;
42 volatile bool QueryCancelPending = false;
43 volatile bool ProcDiePending = false;
44 volatile bool ImmediateInterruptOK = false;
45 volatile uint32 InterruptHoldoffCount = 0;
46 volatile uint32 CritSectionCount = 0;
48 bool IsUnderPostmaster = false;
49 bool assert_enabled = true;
51 int MaxBackends = 32;
52 int NBuffers = 64;
54 char *DataDir = ".";
57 #define MAX_ON_EXITS 20
59 static struct ONEXIT
61 pg_on_exit_callback function;
62 Datum arg;
63 } on_proc_exit_list[MAX_ON_EXITS], on_shmem_exit_list[MAX_ON_EXITS];
65 static int on_proc_exit_index,
66 on_shmem_exit_index;
68 void
69 proc_exit(int code)
71 shmem_exit(code);
72 while (--on_proc_exit_index >= 0)
73 (*on_proc_exit_list[on_proc_exit_index].function) (code,
74 on_proc_exit_list[on_proc_exit_index].arg);
75 exit(code);
78 void
79 shmem_exit(int code)
81 while (--on_shmem_exit_index >= 0)
82 (*on_shmem_exit_list[on_shmem_exit_index].function) (code,
83 on_shmem_exit_list[on_shmem_exit_index].arg);
84 on_shmem_exit_index = 0;
87 void
88 on_shmem_exit(pg_on_exit_callback function, Datum arg)
90 if (on_shmem_exit_index >= MAX_ON_EXITS)
91 elog(FATAL, "out of on_shmem_exit slots");
93 on_shmem_exit_list[on_shmem_exit_index].function = function;
94 on_shmem_exit_list[on_shmem_exit_index].arg = arg;
96 ++on_shmem_exit_index;
99 void
100 on_exit_reset(void)
102 on_shmem_exit_index = 0;
103 on_proc_exit_index = 0;
106 void
107 RecordSharedMemoryInLockFile(unsigned long id1, unsigned long id2)
111 void
112 ProcessInterrupts(void)
117 ExceptionalCondition(const char *conditionName,
118 const char *errorType,
119 const char *fileName,
120 int lineNumber)
122 fprintf(stderr, "TRAP: %s(\"%s\", File: \"%s\", Line: %d)\n",
123 errorType, conditionName,
124 fileName, lineNumber);
125 abort();
126 return 0;
131 errcode_for_file_access(void)
133 return 0;
136 bool
137 errstart(int elevel, const char *filename, int lineno,
138 const char *funcname)
140 return (elevel >= ERROR);
143 void
144 errfinish(int dummy,...)
146 proc_exit(1);
149 void
150 elog_start(const char *filename, int lineno, const char *funcname)
154 void
155 elog_finish(int elevel, const char *fmt,...)
157 fprintf(stderr, "ERROR: %s\n", fmt);
158 proc_exit(1);
162 errcode(int sqlerrcode)
164 return 0; /* return value does not matter */
168 errmsg(const char *fmt,...)
170 fprintf(stderr, "ERROR: %s\n", fmt);
171 return 0; /* return value does not matter */
175 errmsg_internal(const char *fmt,...)
177 fprintf(stderr, "ERROR: %s\n", fmt);
178 return 0; /* return value does not matter */
182 errdetail(const char *fmt,...)
184 fprintf(stderr, "DETAIL: %s\n", fmt);
185 return 0; /* return value does not matter */
189 errdetail_log(const char *fmt,...)
191 fprintf(stderr, "DETAIL: %s\n", fmt);
192 return 0; /* return value does not matter */
196 errhint(const char *fmt,...)
198 fprintf(stderr, "HINT: %s\n", fmt);
199 return 0; /* return value does not matter */
203 /********* here's the actual test *********/
206 typedef struct MyStorage
208 PGShmemHeader header;
209 int flag;
210 PGSemaphoreData sem;
211 } MyStorage;
215 main(int argc, char **argv)
217 MyStorage *storage;
218 int cpid;
220 printf("Creating shared memory ... ");
221 fflush(stdout);
223 storage = (MyStorage *) PGSharedMemoryCreate(8192, false, 5433);
225 storage->flag = 1234;
227 printf("OK\n");
229 printf("Creating semaphores ... ");
230 fflush(stdout);
232 PGReserveSemaphores(2, 5433);
234 PGSemaphoreCreate(&storage->sem);
236 printf("OK\n");
238 /* sema initial value is 1, so lock should work */
240 printf("Testing Lock ... ");
241 fflush(stdout);
243 PGSemaphoreLock(&storage->sem, false);
245 printf("OK\n");
247 /* now sema value is 0, so trylock should fail */
249 printf("Testing TryLock ... ");
250 fflush(stdout);
252 if (PGSemaphoreTryLock(&storage->sem))
253 printf("unexpected result!\n");
254 else
255 printf("OK\n");
257 /* unlocking twice and then locking twice should work... */
259 printf("Testing Multiple Lock ... ");
260 fflush(stdout);
262 PGSemaphoreUnlock(&storage->sem);
263 PGSemaphoreUnlock(&storage->sem);
265 PGSemaphoreLock(&storage->sem, false);
266 PGSemaphoreLock(&storage->sem, false);
268 printf("OK\n");
270 /* check Reset too */
272 printf("Testing Reset ... ");
273 fflush(stdout);
275 PGSemaphoreUnlock(&storage->sem);
277 PGSemaphoreReset(&storage->sem);
279 if (PGSemaphoreTryLock(&storage->sem))
280 printf("unexpected result!\n");
281 else
282 printf("OK\n");
284 /* Fork a child process and see if it can communicate */
286 printf("Forking child process ... ");
287 fflush(stdout);
289 cpid = fork();
290 if (cpid == 0)
292 /* In child */
293 on_exit_reset();
294 sleep(3);
295 storage->flag++;
296 PGSemaphoreUnlock(&storage->sem);
297 proc_exit(0);
299 if (cpid < 0)
301 /* Fork failed */
302 printf("failed: %s\n", strerror(errno));
303 proc_exit(1);
306 printf("forked child pid %d OK\n", cpid);
308 if (storage->flag != 1234)
309 printf("Wrong value found in shared memory!\n");
311 printf("Waiting for child (should wait 3 sec here) ... ");
312 fflush(stdout);
314 PGSemaphoreLock(&storage->sem, false);
316 printf("OK\n");
318 if (storage->flag != 1235)
319 printf("Wrong value found in shared memory!\n");
321 /* Test shutdown */
323 printf("Running shmem_exit processing ... ");
324 fflush(stdout);
326 shmem_exit(0);
328 printf("OK\n");
330 printf("Tests complete.\n");
332 proc_exit(0);
334 return 0; /* not reached */