]> git.8kb.co.uk Git - pgpool-ii/pgpool-ii_2.2.5/blob - sql/pgpool-recovery/pgpool-recovery.c
Attempt to send a proper failure message to frontend when authentication
[pgpool-ii/pgpool-ii_2.2.5] / sql / pgpool-recovery / pgpool-recovery.c
1 /* -*-pgsql-c-*- */
2 /*
3  * $Header: /cvsroot/pgpool/pgpool-II/sql/pgpool-recovery/pgpool-recovery.c,v 1.6.2.2 2009/07/14 08:42:24 t-ishii Exp $
4  *
5  * pgpool-recovery: exec online recovery script from SELECT statement.
6  *
7  * Copyright (c) 2003-2009      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
21 #include "postgres.h"
22 #include "fmgr.h"
23 #include "miscadmin.h"
24 #include "executor/spi.h"
25 #include "funcapi.h"
26 #include "utils/builtins.h"             /* PostgreSQL 8.4 needs this for textout */
27
28 #define REMOTE_START_FILE "pgpool_remote_start"
29
30 #include <stdlib.h>
31
32 #ifdef PG_MODULE_MAGIC
33 PG_MODULE_MAGIC;
34 #endif
35
36 PG_FUNCTION_INFO_V1(pgpool_recovery);
37 PG_FUNCTION_INFO_V1(pgpool_remote_start);
38
39 extern Datum pgpool_recovery(PG_FUNCTION_ARGS);
40 extern Datum pgpool_remote_start(PG_FUNCTION_ARGS);
41
42 static char recovery_script[1024];
43
44 Datum
45 pgpool_recovery(PG_FUNCTION_ARGS)
46 {
47         int r;
48         char *script = DatumGetCString(DirectFunctionCall1(textout,
49                                                                                                            PointerGetDatum(PG_GETARG_TEXT_P(0))));
50                                                                                                            
51         char *remote_host = DatumGetCString(DirectFunctionCall1(textout,
52                                                                                                                         PointerGetDatum(PG_GETARG_TEXT_P(1))));
53         char *remote_data_directory = DatumGetCString(DirectFunctionCall1(textout,
54                                                                                                                                           PointerGetDatum(PG_GETARG_TEXT_P(2))));
55
56         if (!superuser())
57                 ereport(ERROR,
58                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
59                                  (errmsg("must be superuser to use pgpool_recovery function"))));
60
61         snprintf(recovery_script, sizeof(recovery_script), "%s/%s %s %s %s",
62                          DataDir, script, DataDir, remote_host,
63                          remote_data_directory);
64         elog(DEBUG1, "recovery_script: %s", recovery_script);
65         r = system(recovery_script);
66
67         if (r != 0)
68         {
69                 elog(ERROR, "pgpool_recovery failed");
70         }
71
72         PG_RETURN_BOOL(true);
73 }
74
75
76 Datum
77 pgpool_remote_start(PG_FUNCTION_ARGS)
78 {
79         int r;
80         char *remote_host = DatumGetCString(DirectFunctionCall1(textout,
81                                                                                                                         PointerGetDatum(PG_GETARG_TEXT_P(0))));
82         char *remote_data_directory = DatumGetCString(DirectFunctionCall1(textout,
83                                                                                                                                           PointerGetDatum(PG_GETARG_TEXT_P(1))));
84
85         if (!superuser())
86                 ereport(ERROR,
87                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
88                                  (errmsg("must be superuser to use pgpool_remote_start function"))));
89
90         snprintf(recovery_script, sizeof(recovery_script),
91                          "%s/%s %s %s", DataDir, REMOTE_START_FILE,
92                          remote_host, remote_data_directory);
93         elog(DEBUG1, "recovery_script: %s", recovery_script);
94         r = system(recovery_script);
95
96         if (r != 0)
97         {
98                 elog(ERROR, "pgpool_remote_start failed");
99         }
100
101         PG_RETURN_BOOL(true);
102 }