1
2# Copyright (c) 2021, PostgreSQL Global Development Group
3
4use strict;
5use warnings;
6
7use Config;
8use Fcntl ':mode';
9use File::stat qw{lstat};
10use PostgresNode;
11use TestLib;
12use Test::More tests => 24;
13
14my $tempdir       = TestLib::tempdir;
15my $tempdir_short = TestLib::tempdir_short;
16
17program_help_ok('pg_ctl');
18program_version_ok('pg_ctl');
19program_options_handling_ok('pg_ctl');
20
21command_exit_is([ 'pg_ctl', 'start', '-D', "$tempdir/nonexistent" ],
22	1, 'pg_ctl start with nonexistent directory');
23
24command_ok([ 'pg_ctl', 'initdb', '-D', "$tempdir/data", '-o', '-N' ],
25	'pg_ctl initdb');
26command_ok([ $ENV{PG_REGRESS}, '--config-auth', "$tempdir/data" ],
27	'configure authentication');
28my $node_port = get_free_port();
29open my $conf, '>>', "$tempdir/data/postgresql.conf";
30print $conf "fsync = off\n";
31print $conf "port = $node_port\n";
32print $conf TestLib::slurp_file($ENV{TEMP_CONFIG})
33  if defined $ENV{TEMP_CONFIG};
34
35if ($use_unix_sockets)
36{
37	print $conf "listen_addresses = ''\n";
38	print $conf "unix_socket_directories = '$tempdir_short'\n";
39}
40else
41{
42	print $conf "listen_addresses = '127.0.0.1'\n";
43}
44close $conf;
45my $ctlcmd = [
46	'pg_ctl', 'start', '-D', "$tempdir/data", '-l',
47	"$TestLib::log_path/001_start_stop_server.log"
48];
49if ($Config{osname} ne 'msys')
50{
51	command_like($ctlcmd, qr/done.*server started/s, 'pg_ctl start');
52}
53else
54{
55
56	# use the version of command_like that doesn't hang on Msys here
57	command_like_safe($ctlcmd, qr/done.*server started/s, 'pg_ctl start');
58}
59
60# sleep here is because Windows builds can't check postmaster.pid exactly,
61# so they may mistake a pre-existing postmaster.pid for one created by the
62# postmaster they start.  Waiting more than the 2 seconds slop time allowed
63# by wait_for_postmaster() prevents that mistake.
64sleep 3 if ($windows_os);
65command_fails([ 'pg_ctl', 'start', '-D', "$tempdir/data" ],
66	'second pg_ctl start fails');
67command_ok([ 'pg_ctl', 'stop', '-D', "$tempdir/data" ], 'pg_ctl stop');
68command_fails([ 'pg_ctl', 'stop', '-D', "$tempdir/data" ],
69	'second pg_ctl stop fails');
70
71# Log file for default permission test.  The permissions won't be checked on
72# Windows but we still want to do the restart test.
73my $logFileName = "$tempdir/data/perm-test-600.log";
74
75command_ok([ 'pg_ctl', 'restart', '-D', "$tempdir/data", '-l', $logFileName ],
76	'pg_ctl restart with server not running');
77
78# Permissions on log file should be default
79SKIP:
80{
81	skip "unix-style permissions not supported on Windows", 2
82	  if ($windows_os);
83
84	ok(-f $logFileName);
85	ok(check_mode_recursive("$tempdir/data", 0700, 0600));
86}
87
88# Log file for group access test
89$logFileName = "$tempdir/data/perm-test-640.log";
90
91SKIP:
92{
93	skip "group access not supported on Windows", 3 if ($windows_os);
94
95	system_or_bail 'pg_ctl', 'stop', '-D', "$tempdir/data";
96
97	# Change the data dir mode so log file will be created with group read
98	# privileges on the next start
99	chmod_recursive("$tempdir/data", 0750, 0640);
100
101	command_ok(
102		[ 'pg_ctl', 'start', '-D', "$tempdir/data", '-l', $logFileName ],
103		'start server to check group permissions');
104
105	ok(-f $logFileName);
106	ok(check_mode_recursive("$tempdir/data", 0750, 0640));
107}
108
109command_ok([ 'pg_ctl', 'restart', '-D', "$tempdir/data" ],
110	'pg_ctl restart with server running');
111
112system_or_bail 'pg_ctl', 'stop', '-D', "$tempdir/data";
113