7
7
#include "pg_probackup.h"
8
8
#include "file.h"
9
9
10
- #define MAX_CMDLINE_LENGTH 4096
10
+ #define MAX_CMDLINE_LENGTH 4096
11
+ #define MAX_CMDLINE_OPTIONS 256
11
12
12
13
static int append_option (char * buf , size_t buf_size , size_t dst , char const * src )
13
14
{
@@ -21,11 +22,50 @@ static int append_option(char* buf, size_t buf_size, size_t dst, char const* src
21
22
return dst + len + 1 ;
22
23
}
23
24
25
+ static int split_options (int argc , char * argv [], int max_options , char * options )
26
+ {
27
+ char * opt = options ;
28
+ char in_quote = '\0' ;
29
+ while (true) {
30
+ switch (* opt ) {
31
+ case '\'' :
32
+ case '\"' :
33
+ if (!in_quote ) {
34
+ in_quote = * opt ++ ;
35
+ continue ;
36
+ }
37
+ if (* opt == in_quote && * ++ opt != in_quote ) {
38
+ in_quote = '\0' ;
39
+ continue ;
40
+ }
41
+ break ;
42
+ case '\0' :
43
+ if (opt != options ) {
44
+ argv [argc ++ ] = options ;
45
+ if (argc >= max_options )
46
+ elog (ERROR , "Too much options" );
47
+ }
48
+ return argc ;
49
+ case ' ' :
50
+ argv [argc ++ ] = options ;
51
+ if (argc >= max_options )
52
+ elog (ERROR , "Too much options" );
53
+ * opt ++ = '\0' ;
54
+ options = opt ;
55
+ continue ;
56
+ default :
57
+ break ;
58
+ }
59
+ opt += 1 ;
60
+ }
61
+ return argc ;
62
+ }
63
+
24
64
int remote_execute (int argc , char * argv [], bool listen )
25
65
{
26
66
char cmd [MAX_CMDLINE_LENGTH ];
27
67
size_t dst = 0 ;
28
- char * ssh_argv [8 ];
68
+ char * ssh_argv [MAX_CMDLINE_OPTIONS ];
29
69
int ssh_argc ;
30
70
int i ;
31
71
int outfd [2 ];
@@ -34,14 +74,17 @@ int remote_execute(int argc, char* argv[], bool listen)
34
74
35
75
ssh_argc = 0 ;
36
76
ssh_argv [ssh_argc ++ ] = remote_proto ;
37
- if (remote_port != 0 ) {
77
+ if (remote_port != NULL ) {
38
78
ssh_argv [ssh_argc ++ ] = (char * )"-p" ;
39
79
ssh_argv [ssh_argc ++ ] = remote_port ;
40
80
}
41
- if (ssh_config != 0 ) {
81
+ if (ssh_config != NULL ) {
42
82
ssh_argv [ssh_argc ++ ] = (char * )"-F" ;
43
83
ssh_argv [ssh_argc ++ ] = ssh_config ;
44
84
}
85
+ if (ssh_options != NULL ) {
86
+ ssh_argc = split_options (ssh_argc , ssh_argv , MAX_CMDLINE_OPTIONS , ssh_options );
87
+ }
45
88
ssh_argv [ssh_argc ++ ] = remote_host ;
46
89
ssh_argv [ssh_argc ++ ] = cmd + 1 ;
47
90
ssh_argv [ssh_argc ] = NULL ;
0 commit comments