1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
/* Debian wrapper program for PostgreSQL front-ends.
Copyright (c) Oliver Elphick 1998
Licensing: Anyone may use this code under the same terms as the
Debian PostgreSQL database package code, of which it is
a part and with which it is released.
This program is used to set the envelope from the configuration in
/etc/postgresql/postmaster, so that programs know where to find
the PostgreSQL library and database.
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#define DO_NOT_OVERWRITE 0
void read_env();
void no_database();
char * progname;
int main (int argc, char **argv)
{
char cmd[128] = "/usr/lib/postgresql/bin/\0";
char *dbname = NULL, *p, **q;
progname = argv[0];
read_env();
/* Write the default values into the environment */
setenv("PGPORT", "5432", DO_NOT_OVERWRITE);
setenv("PAGER", "/usr/bin/pager", DO_NOT_OVERWRITE);
/* identify the command */
if ((p = rindex(argv[0], '/')))
{
++p;
} else {
p = argv[0];
}
strncat (cmd, p, 100);
argv[0] = rindex(cmd, '/') + 1;
/* This program should not be called as `pg_wrapper' */
if (!strcmp(argv[0],"pg_wrapper"))
{
fprintf(stderr,"pg_wrapper cannot be run as itself, but only through a link\nwhose name is that of the real program to be run.\n");
exit(1);
}
/* Special processing for psql */
if (!strcmp(argv[0],"psql"))
{
int ix = 0;
/* Check the command line */
char *z[128];
char newopt[3] = "--";
int c;
static struct option long_options[] =
{
{"echo-all", no_argument, 0, 'a'},
{"no-align", no_argument, 0, 'A'},
{"command", required_argument, 0, 'c'},
{"dbname", required_argument, 0, 'd'},
{"echo-queries", no_argument, 0, 'e'},
{"echo-hidden", no_argument, 0, 'E'},
{"file", required_argument, 0, 'f'},
{"field-separator", required_argument, 0, 'F'},
{"host", required_argument, 0, 'h'},
{"html", no_argument, 0, 'H'},
{"list", no_argument, 0, 'l'},
{"no-readline", no_argument, 0, 'n'},
{"output", required_argument, 0, 'o'},
{"port", required_argument, 0, 'p'},
{"pset", required_argument, 0, 'P'},
{"quiet", no_argument, 0, 'q'},
{"record-separator", required_argument, 0, 'R'},
{"single-step", no_argument, 0, 's'},
{"single-line", no_argument, 0, 'S'},
{"tuples-only", no_argument, 0, 't'},
{"table-attr", required_argument, 0, 'T'},
{"username", required_argument, 0, 'U'},
{"variable", required_argument, 0, 'v'},
{"set", required_argument, 0, 'v'},
{"version", no_argument, 0, 'V'},
{"password", no_argument, 0, 'W'},
{"expanded", no_argument, 0, 'x'},
{"no-psqlrc", no_argument, 0, 'X'},
{"help", no_argument, 0, '?'},
{0, 0, 0, 0}
};
int option_index = 0;
while ((c = getopt_long(argc, argv, "aAc:d:Eef:F:h:Hlno:p:P:qR:sStT:uU:v:VWxX?", long_options, &option_index)) != EOF)
{
switch (c)
{
case 'd':
dbname = strdup(argv[optind-1]);
break;
case 'c':
case 'f':
case 'F':
case 'h':
case 'o':
case 'P':
case 'p':
case 'R':
case 'T':
case 'U':
case 'v':
/* add option and argument to command line */
newopt[1] = c;
z[++ix] = strdup(newopt);
z[++ix] = strdup(optarg);
break;
case 'l':
case 'V':
dbname = strdup("template1");
case 'a':
case 'A':
case 'e':
case 'E':
case 'H':
case 'n':
case 'q':
case 's':
case 'S':
case 't':
case 'u':
case 'W':
case 'x':
case 'X':
case '?':
/* add option to command line */
newopt[1] = c;
z[++ix] = strdup(newopt);
if (c == '?')
{
z[++ix] = NULL;
q = z;
goto run_command; /* yuk - a goto!!! */
}
break;
default:
/* an error message has been printed by getopt() */
exit(1);
}
}
if (!dbname && argv[optind] != NULL)
{
dbname = strdup(argv[optind++]);
if (argv[optind] != NULL)
{
/* Remaining argument is the user name */
z[++ix] = "-U";
z[++ix] = argv[optind];
}
}
if (!dbname)
{
char * pgdatabase;
pgdatabase = getenv("PGDATABASE");
if (pgdatabase)
{
int l;
l = strlen(pgdatabase);
if (l)
{
dbname = malloc(++l);
strcpy(dbname, pgdatabase);
} else {
no_database();
}
} else {
no_database();
}
}
z[++ix] = "-d";
z[++ix] = dbname;
z[++ix] = NULL;
q = z;
} else {
q = argv;
}
run_command:
/* Run the real command */
q[0] = cmd;
execv(cmd, q);
/* If we get here, the execv() failed */
fprintf(stderr, "Could not execv %s\n", cmd);
return(255);
}
void no_database()
{
fprintf(stderr, "%s (pg_wrapper): No database specified\n",
progname);
exit(1);
}
/* read_env()
* Read the default environment from postgresql.env
*/
void
read_env()
{
char *evar = NULL;
char *value = NULL;
char cmd[] = "env - /usr/lib/postgresql/bin/readpgenv";
size_t sz = 0, sz2 = 0;
FILE *input;
fd_set fds;
input = popen(cmd, "r");
if (!input)
{
fprintf(stderr,"Could not run %s\n", cmd);
exit(1);
}
FD_ZERO(&fds);
FD_SET(fileno(input), &fds);
select(fileno(input) + 1, &fds, NULL, NULL, NULL);
while (getdelim(&evar, &sz, '=', input) >= 0)
{
if (getdelim(&value, &sz2, '\n', input) >= 0)
{
if (!strcmp(evar,"_"))
{
setenv(evar, value, DO_NOT_OVERWRITE);
}
} else {
fprintf(stderr, "Unexpected input from %s:\n%s=%s\n", cmd, evar, value);
exit(2);
}
}
pclose(input);
}
|