misc.c 4.7 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * misc.c
4
 *
5
 *
B
Bruce Momjian 已提交
6
 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
11
 *	  $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.36 2004/08/03 20:32:33 tgl Exp $
12 13 14
 *
 *-------------------------------------------------------------------------
 */
15 16
#include "postgres.h"

17
#include <sys/file.h>
18
#include <signal.h>
J
Joe Conway 已提交
19
#include <dirent.h>
20

T
Tom Lane 已提交
21
#include "commands/dbcommands.h"
B
 
Bruce Momjian 已提交
22
#include "miscadmin.h"
23
#include "storage/sinval.h"
J
Joe Conway 已提交
24
#include "storage/fd.h"
25
#include "utils/builtins.h"
J
Joe Conway 已提交
26 27 28
#include "funcapi.h"
#include "catalog/pg_type.h"
#include "catalog/pg_tablespace.h"
29

30 31
#define atooid(x)  ((Oid) strtoul((x), NULL, 10))

32 33

/*
34
 * Check if data is Null
35
 */
36 37
Datum
nullvalue(PG_FUNCTION_ARGS)
38
{
39 40 41
	if (PG_ARGISNULL(0))
		PG_RETURN_BOOL(true);
	PG_RETURN_BOOL(false);
42 43
}

44 45 46 47 48
/*
 * Check if data is not Null
 */
Datum
nonnullvalue(PG_FUNCTION_ARGS)
49
{
50 51 52
	if (PG_ARGISNULL(0))
		PG_RETURN_BOOL(false);
	PG_RETURN_BOOL(true);
53 54
}

B
 
Bruce Momjian 已提交
55 56 57 58 59 60 61
/*
 * current_database()
 *	Expose the current database to the user
 */
Datum
current_database(PG_FUNCTION_ARGS)
{
B
Bruce Momjian 已提交
62
	Name		db;
B
 
Bruce Momjian 已提交
63 64 65

	db = (Name) palloc(NAMEDATALEN);

66
	namestrcpy(db, get_database_name(MyDatabaseId));
B
 
Bruce Momjian 已提交
67 68
	PG_RETURN_NAME(db);
}
69 70 71


/*
72
 * Functions to send signals to other backends.
73 74 75 76 77 78 79
 */

static int pg_signal_backend(int pid, int sig) 
{
	if (!superuser()) 
		ereport(ERROR,
				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
80
				 (errmsg("must be superuser to signal other server processes"))));
81 82 83
	
	if (!IsBackendPid(pid))
	{
84 85 86 87
		/*
		 * This is just a warning so a loop-through-resultset will not abort
		 * if one backend terminated on it's own during the run
		 */
88
		ereport(WARNING,
89
				(errmsg("PID %d is not a PostgreSQL server process", pid)));
90 91 92 93 94 95 96
		return 0;
	}

	if (kill(pid, sig)) 
	{
		/* Again, just a warning to allow loops */
		ereport(WARNING,
97
				(errmsg("could not send signal to process %d: %m",pid)));
98 99 100 101 102 103
		return 0;
	}
	return 1;
}

Datum
104
pg_cancel_backend(PG_FUNCTION_ARGS)
105
{
106
	PG_RETURN_INT32(pg_signal_backend(PG_GETARG_INT32(0),SIGINT));
107 108
}

109 110 111 112
#ifdef NOT_USED

/* Disabled in 8.0 due to reliability concerns; FIXME someday */

113
Datum
114
pg_terminate_backend(PG_FUNCTION_ARGS)
115
{
116
	PG_RETURN_INT32(pg_signal_backend(PG_GETARG_INT32(0),SIGTERM));
117
}
J
Joe Conway 已提交
118

119 120 121 122
#endif


/* Function to find out which databases make use of a tablespace */
J
Joe Conway 已提交
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

typedef struct 
{
	char *location;
	DIR *dirdesc;
} ts_db_fctx;

Datum pg_tablespace_databases(PG_FUNCTION_ARGS)
{
	FuncCallContext *funcctx;
	struct dirent *de;
	ts_db_fctx *fctx;

	if (SRF_IS_FIRSTCALL())
	{
		MemoryContext oldcontext;
		Oid tablespaceOid=PG_GETARG_OID(0);

		funcctx=SRF_FIRSTCALL_INIT();
		oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);

		fctx = palloc(sizeof(ts_db_fctx));

		/*
		 * size = path length + tablespace dirname length
		 *        + 2 dir sep chars + oid + terminator
		 */
		fctx->location = (char*) palloc(strlen(DataDir) + 11 + 10 + 1);
		if (tablespaceOid == GLOBALTABLESPACE_OID)
		{
			fctx->dirdesc = NULL;
154 155
			ereport(WARNING,
					(errmsg("global tablespace never has databases")));
J
Joe Conway 已提交
156 157 158 159 160 161 162 163 164 165 166
		}
		else
		{
			if (tablespaceOid == DEFAULTTABLESPACE_OID)
				sprintf(fctx->location, "%s/base", DataDir);
			else
				sprintf(fctx->location, "%s/pg_tblspc/%u", DataDir,
														   tablespaceOid);
		
			fctx->dirdesc = AllocateDir(fctx->location);

167 168 169 170 171 172 173 174 175 176 177
			if (!fctx->dirdesc)
			{
				/* the only expected error is ENOENT */
				if (errno != ENOENT)
					ereport(ERROR,
							(errcode_for_file_access(),
							 errmsg("could not open directory \"%s\": %m",
									fctx->location)));
				ereport(WARNING,
						(errmsg("%u is not a tablespace oid", tablespaceOid)));
			}
J
Joe Conway 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
		}
		funcctx->user_fctx = fctx;
		MemoryContextSwitchTo(oldcontext);
	}

	funcctx=SRF_PERCALL_SETUP();
	fctx = (ts_db_fctx*) funcctx->user_fctx;

	if (!fctx->dirdesc)  /* not a tablespace */
		SRF_RETURN_DONE(funcctx);

	while ((de = readdir(fctx->dirdesc)) != NULL)
	{
		char *subdir;
		DIR *dirdesc;

194 195
		Oid datOid = atooid(de->d_name);
		/* this test skips . and .., but is awfully weak */
J
Joe Conway 已提交
196 197 198
		if (!datOid)
			continue;

199 200
		/* if database subdir is empty, don't report tablespace as used */

J
Joe Conway 已提交
201 202 203 204
		/* size = path length + dir sep char + file name + terminator */
		subdir = palloc(strlen(fctx->location) + 1 + strlen(de->d_name) + 1);
		sprintf(subdir, "%s/%s", fctx->location, de->d_name);
		dirdesc = AllocateDir(subdir);
205 206 207
		pfree(subdir);
		if (!dirdesc)
			continue;			/* XXX more sloppiness */
J
Joe Conway 已提交
208

209 210 211 212
		while ((de = readdir(dirdesc)) != 0)
		{
			if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
				break;
J
Joe Conway 已提交
213
		}
214 215 216 217
		FreeDir(dirdesc);

		if (!de)
			continue;			/* indeed, nothing in it */
J
Joe Conway 已提交
218 219 220 221 222 223 224

		SRF_RETURN_NEXT(funcctx, ObjectIdGetDatum(datOid));
	}

	FreeDir(fctx->dirdesc);
	SRF_RETURN_DONE(funcctx);
}