Skip to content

Commit b70982e

Browse files
authored
Review and fix the initialize_program API. (#688)
1 parent e069cfd commit b70982e

8 files changed

Lines changed: 77 additions & 64 deletions

File tree

src/bin/lib/subcommands.c/runprogram.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ typedef struct
5252
} Program;
5353

5454
Program run_program(const char *program, ...);
55-
Program initialize_program(char **args, bool setsid);
55+
void initialize_program(Program *prog, char **args, bool setsid);
5656
void execute_subprogram(Program *prog);
5757
void execute_program(Program *prog);
5858
void free_program(Program *prog);
@@ -132,42 +132,42 @@ run_program(const char *program, ...)
132132
* caller to manipulate the structure for itself. Safe to change are program,
133133
* args and setsid structure slots.
134134
*/
135-
Program
136-
initialize_program(char **args, bool setsid)
135+
void
136+
initialize_program(Program *prog, char **args, bool setsid)
137137
{
138138
int argsIndex, nb_args = 0;
139-
Program prog = { 0 };
140139

141-
prog.returnCode = -1;
142-
prog.error = 0;
143-
prog.setsid = setsid;
140+
/* we want to have a deterministic starting point */
141+
*prog = (Program) { 0 };
142+
143+
prog->returnCode = -1;
144+
prog->error = 0;
145+
prog->setsid = setsid;
144146

145147
/* this could be changed by the caller before calling execute_program */
146-
prog.capture = true;
147-
prog.tty = false;
148-
prog.processBuffer = NULL;
149-
prog.stdOutFd = -1;
150-
prog.stdErrFd = -1;
148+
prog->capture = true;
149+
prog->tty = false;
150+
prog->processBuffer = NULL;
151+
prog->stdOutFd = -1;
152+
prog->stdErrFd = -1;
151153

152-
prog.stdOut = NULL;
153-
prog.stdErr = NULL;
154+
prog->stdOut = NULL;
155+
prog->stdErr = NULL;
154156

155157
for (argsIndex = 0; args[argsIndex] != NULL; argsIndex++)
156158
{
157159
++nb_args;
158160
}
159161

160162
/* add another one nb_args for the terminating NULL entry */
161-
prog.args = (char **) malloc(++nb_args * sizeof(char *));
162-
memset(prog.args, 0, nb_args * sizeof(char *));
163+
prog->args = (char **) malloc(++nb_args * sizeof(char *));
164+
memset(prog->args, 0, nb_args * sizeof(char *));
163165

164166
for (argsIndex = 0; args[argsIndex] != NULL; argsIndex++)
165167
{
166-
prog.args[argsIndex] = strdup(args[argsIndex]);
168+
prog->args[argsIndex] = strdup(args[argsIndex]);
167169
}
168-
prog.program = prog.args[0];
169-
170-
return prog;
170+
prog->program = prog->args[0];
171171
}
172172

173173

src/bin/pg_autoctl/azure.c

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,13 @@ azure_psleep(int count, bool force)
343343
char *args[3];
344344
int argsIndex = 0;
345345

346-
Program program;
346+
Program program = { 0 };
347347

348348
args[argsIndex++] = sleep;
349349
args[argsIndex++] = "5";
350350
args[argsIndex++] = NULL;
351351

352-
program = initialize_program(args, false);
352+
(void) initialize_program(&program, args, false);
353353

354354
pidArray[i] = azure_start_command(&program);
355355
}
@@ -413,7 +413,7 @@ azure_create_group(const char *name, const char *location)
413413
char *args[16];
414414
int argsIndex = 0;
415415

416-
Program program;
416+
Program program = { 0 };
417417

418418
args[argsIndex++] = azureCLI;
419419
args[argsIndex++] = "group";
@@ -424,7 +424,7 @@ azure_create_group(const char *name, const char *location)
424424
args[argsIndex++] = (char *) location;
425425
args[argsIndex++] = NULL;
426426

427-
program = initialize_program(args, false);
427+
(void) initialize_program(&program, args, false);
428428

429429
log_info("Creating group \"%s\" in location \"%s\"", name, location);
430430

@@ -441,7 +441,7 @@ azure_create_vnet(const char *group, const char *name, const char *prefix)
441441
char *args[16];
442442
int argsIndex = 0;
443443

444-
Program program;
444+
Program program = { 0 };
445445

446446
args[argsIndex++] = azureCLI;
447447
args[argsIndex++] = "network";
@@ -455,7 +455,7 @@ azure_create_vnet(const char *group, const char *name, const char *prefix)
455455
args[argsIndex++] = (char *) prefix;
456456
args[argsIndex++] = NULL;
457457

458-
program = initialize_program(args, false);
458+
(void) initialize_program(&program, args, false);
459459

460460
log_info("Creating network vnet \"%s\" using address prefix \"%s\"",
461461
name, prefix);
@@ -473,7 +473,7 @@ azure_create_nsg(const char *group, const char *name)
473473
char *args[16];
474474
int argsIndex = 0;
475475

476-
Program program;
476+
Program program = { 0 };
477477

478478
args[argsIndex++] = azureCLI;
479479
args[argsIndex++] = "network";
@@ -485,7 +485,7 @@ azure_create_nsg(const char *group, const char *name)
485485
args[argsIndex++] = (char *) name;
486486
args[argsIndex++] = NULL;
487487

488-
program = initialize_program(args, false);
488+
(void) initialize_program(&program, args, false);
489489

490490
log_info("Creating network nsg \"%s\"", name);
491491

@@ -505,7 +505,7 @@ azure_create_nsg_rule(const char *group,
505505
char *args[38];
506506
int argsIndex = 0;
507507

508-
Program program;
508+
Program program = { 0 };
509509

510510
args[argsIndex++] = azureCLI;
511511
args[argsIndex++] = "network";
@@ -537,7 +537,7 @@ azure_create_nsg_rule(const char *group,
537537
args[argsIndex++] = "5432";
538538
args[argsIndex++] = NULL;
539539

540-
program = initialize_program(args, false);
540+
(void) initialize_program(&program, args, false);
541541

542542
log_info("Creating network nsg rules \"%s\" for our IP address \"%s\" "
543543
"for ports 22 and 5432", name, ipAddress);
@@ -559,7 +559,7 @@ azure_create_subnet(const char *group,
559559
char *args[16];
560560
int argsIndex = 0;
561561

562-
Program program;
562+
Program program = { 0 };
563563

564564
args[argsIndex++] = azureCLI;
565565
args[argsIndex++] = "network";
@@ -578,7 +578,7 @@ azure_create_subnet(const char *group,
578578
args[argsIndex++] = (char *) nsg;
579579
args[argsIndex++] = NULL;
580580

581-
program = initialize_program(args, false);
581+
(void) initialize_program(&program, args, false);
582582

583583
log_info("Creating network subnet \"%s\" using address prefix \"%s\"",
584584
name, prefixes);
@@ -596,7 +596,7 @@ az_group_delete(const char *group)
596596
char *args[16];
597597
int argsIndex = 0;
598598

599-
Program program;
599+
Program program = { 0 };
600600

601601
args[argsIndex++] = azureCLI;
602602
args[argsIndex++] = "group";
@@ -606,7 +606,7 @@ az_group_delete(const char *group)
606606
args[argsIndex++] = "--yes";
607607
args[argsIndex++] = NULL;
608608

609-
program = initialize_program(args, false);
609+
(void) initialize_program(&program, args, false);
610610

611611
log_info("Deleting azure resource group \"%s\"", group);
612612

@@ -722,7 +722,7 @@ azure_create_vm(AzureRegionResources *azRegion,
722722
char *args[26];
723723
int argsIndex = 0;
724724

725-
Program program;
725+
Program program = { 0 };
726726

727727
char publicIpAddressName[BUFSIZE] = { 0 };
728728

@@ -750,7 +750,7 @@ azure_create_vm(AzureRegionResources *azRegion,
750750
args[argsIndex++] = "--generate-ssh-keys";
751751
args[argsIndex++] = NULL;
752752

753-
program = initialize_program(args, false);
753+
(void) initialize_program(&program, args, false);
754754

755755
log_info("Creating %s virtual machine \"%s\" with user \"%s\"",
756756
image, name, username);
@@ -911,7 +911,7 @@ start_rsync_command(const char *username,
911911
char *args[16];
912912
int argsIndex = 0;
913913

914-
Program program;
914+
Program program = { 0 };
915915

916916
char ssh[MAXPGPATH] = { 0 };
917917
char essh[MAXPGPATH] = { 0 };
@@ -966,7 +966,7 @@ start_rsync_command(const char *username,
966966
args[argsIndex++] = rsync_remote;
967967
args[argsIndex++] = NULL;
968968

969-
program = initialize_program(args, false);
969+
(void) initialize_program(&program, args, false);
970970

971971
return azure_start_command(&program);
972972
}
@@ -1282,7 +1282,7 @@ azure_provision_vm(const char *group, const char *name, bool fromSource)
12821282
char *args[26];
12831283
int argsIndex = 0;
12841284

1285-
Program program;
1285+
Program program = { 0 };
12861286

12871287
char aptGetInstall[BUFSIZE] = { 0 };
12881288
char aptGetInstallPostgres[BUFSIZE] = { 0 };
@@ -1366,7 +1366,7 @@ azure_provision_vm(const char *group, const char *name, bool fromSource)
13661366

13671367
args[argsIndex++] = NULL;
13681368

1369-
program = initialize_program(args, false);
1369+
(void) initialize_program(&program, args, false);
13701370

13711371
log_info("Provisioning Virtual Machine \"%s\"", name);
13721372

@@ -1469,7 +1469,7 @@ azure_resource_list(const char *group)
14691469
int argsIndex = 0;
14701470
bool success = true;
14711471

1472-
Program program;
1472+
Program program = { 0 };
14731473

14741474
char query[BUFSIZE] = { 0 };
14751475

@@ -1489,7 +1489,7 @@ azure_resource_list(const char *group)
14891489
args[argsIndex++] = (char *) query;
14901490
args[argsIndex++] = NULL;
14911491

1492-
program = initialize_program(args, false);
1492+
(void) initialize_program(&program, args, false);
14931493

14941494
(void) snprintf_program_command_line(&program, command, sizeof(command));
14951495

@@ -1523,7 +1523,7 @@ azure_fetch_resource_list(const char *group, AzureRegionResources *azRegion)
15231523
int argsIndex = 0;
15241524
bool success = true;
15251525

1526-
Program program;
1526+
Program program = { 0 };
15271527

15281528
char query[BUFSIZE] = { 0 };
15291529

@@ -1542,7 +1542,7 @@ azure_fetch_resource_list(const char *group, AzureRegionResources *azRegion)
15421542
args[argsIndex++] = (char *) query;
15431543
args[argsIndex++] = NULL;
15441544

1545-
program = initialize_program(args, false);
1545+
(void) initialize_program(&program, args, false);
15461546

15471547
(void) snprintf_program_command_line(&program, command, sizeof(command));
15481548

@@ -1629,7 +1629,7 @@ azure_show_ip_addresses(const char *group)
16291629
int argsIndex = 0;
16301630
bool success = true;
16311631

1632-
Program program;
1632+
Program program = { 0 };
16331633

16341634
char query[BUFSIZE] = { 0 };
16351635

@@ -1653,7 +1653,7 @@ azure_show_ip_addresses(const char *group)
16531653
args[argsIndex++] = "table";
16541654
args[argsIndex++] = NULL;
16551655

1656-
program = initialize_program(args, false);
1656+
(void) initialize_program(&program, args, false);
16571657

16581658
(void) snprintf_program_command_line(&program, command, sizeof(command));
16591659

@@ -1687,7 +1687,7 @@ azure_fetch_ip_addresses(const char *group, AzureVMipAddresses *vmArray)
16871687
int argsIndex = 0;
16881688
bool success = true;
16891689

1690-
Program program;
1690+
Program program = { 0 };
16911691

16921692
char query[BUFSIZE] = { 0 };
16931693

@@ -1711,7 +1711,7 @@ azure_fetch_ip_addresses(const char *group, AzureVMipAddresses *vmArray)
17111711
args[argsIndex++] = "json";
17121712
args[argsIndex++] = NULL;
17131713

1714-
program = initialize_program(args, false);
1714+
(void) initialize_program(&program, args, false);
17151715

17161716
(void) snprintf_program_command_line(&program, command, sizeof(command));
17171717

@@ -1785,7 +1785,7 @@ run_ssh(const char *username, const char *ip)
17851785
char *args[16];
17861786
int argsIndex = 0;
17871787

1788-
Program program;
1788+
Program program = { 0 };
17891789

17901790
char ssh[MAXPGPATH] = { 0 };
17911791
char command[BUFSIZE] = { 0 };
@@ -1808,7 +1808,7 @@ run_ssh(const char *username, const char *ip)
18081808
args[argsIndex++] = (char *) ip;
18091809
args[argsIndex++] = NULL;
18101810

1811-
program = initialize_program(args, false);
1811+
(void) initialize_program(&program, args, false);
18121812

18131813
program.capture = false; /* don't capture output */
18141814
program.tty = true; /* allow sharing the parent's tty */
@@ -1836,7 +1836,7 @@ run_ssh_command(const char *username,
18361836
char *args[16];
18371837
int argsIndex = 0;
18381838

1839-
Program program;
1839+
Program program = { 0 };
18401840

18411841
char ssh[MAXPGPATH] = { 0 };
18421842
char ssh_command[BUFSIZE] = { 0 };
@@ -1867,7 +1867,7 @@ run_ssh_command(const char *username,
18671867
args[argsIndex++] = (char *) command;
18681868
args[argsIndex++] = NULL;
18691869

1870-
program = initialize_program(args, false);
1870+
(void) initialize_program(&program, args, false);
18711871

18721872
program.capture = false; /* don't capture output */
18731873
program.tty = true; /* allow sharing the parent's tty */
@@ -1901,7 +1901,7 @@ start_ssh_command(const char *username,
19011901
char *args[16];
19021902
int argsIndex = 0;
19031903

1904-
Program program;
1904+
Program program = { 0 };
19051905

19061906
char ssh[MAXPGPATH] = { 0 };
19071907
char ssh_command[BUFSIZE] = { 0 };
@@ -1926,7 +1926,7 @@ start_ssh_command(const char *username,
19261926
args[argsIndex++] = (char *) command;
19271927
args[argsIndex++] = NULL;
19281928

1929-
program = initialize_program(args, false);
1929+
(void) initialize_program(&program, args, false);
19301930

19311931
(void) snprintf_program_command_line(&program, ssh_command, BUFSIZE);
19321932

0 commit comments

Comments
 (0)