Skip to content

Commit de61ceb

Browse files
committed
Merge branch 'jk/common-main-2.8' into jk/common-main
* jk/common-main-2.8: mingw: declare main()'s argv as const common-main: call git_setup_gettext() common-main: call restore_sigpipe_to_default() common-main: call sanitize_stdfds() common-main: call git_extract_argv0_path() add an extra level of indirection to main()
2 parents 05219a1 + 08aade7 commit de61ceb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+123
-150
lines changed

Makefile

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ BUILTIN_OBJS += builtin/verify-tag.o
939939
BUILTIN_OBJS += builtin/worktree.o
940940
BUILTIN_OBJS += builtin/write-tree.o
941941

942-
GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
942+
GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB)
943943
EXTLIBS =
944944

945945
GIT_USER_AGENT = git/$(GIT_VERSION)
@@ -1572,7 +1572,15 @@ TCLTK_PATH_SQ = $(subst ','\'',$(TCLTK_PATH))
15721572
DIFF_SQ = $(subst ','\'',$(DIFF))
15731573
PERLLIB_EXTRA_SQ = $(subst ','\'',$(PERLLIB_EXTRA))
15741574

1575-
LIBS = $(GITLIBS) $(EXTLIBS)
1575+
# We must filter out any object files from $(GITLIBS),
1576+
# as it is typically used like:
1577+
#
1578+
# foo: foo.o $(GITLIBS)
1579+
# $(CC) $(filter %.o,$^) $(LIBS)
1580+
#
1581+
# where we use it as a dependency. Since we also pull object files
1582+
# from the dependency list, that would make each entry appear twice.
1583+
LIBS = $(filter-out %.o, $(GITLIBS)) $(EXTLIBS)
15761584

15771585
BASIC_CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER_SQ)' \
15781586
$(COMPAT_CFLAGS)
@@ -1708,8 +1716,8 @@ git.sp git.s git.o: EXTRA_CPPFLAGS = \
17081716
'-DGIT_INFO_PATH="$(infodir_relative_SQ)"'
17091717

17101718
git$X: git.o GIT-LDFLAGS $(BUILTIN_OBJS) $(GITLIBS)
1711-
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) git.o \
1712-
$(BUILTIN_OBJS) $(LIBS)
1719+
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) \
1720+
$(filter %.o,$^) $(LIBS)
17131721

17141722
help.sp help.s help.o: common-cmds.h
17151723

@@ -1902,6 +1910,7 @@ TEST_OBJS := $(patsubst %$X,%.o,$(TEST_PROGRAMS))
19021910
OBJECTS := $(LIB_OBJS) $(BUILTIN_OBJS) $(PROGRAM_OBJS) $(TEST_OBJS) \
19031911
$(XDIFF_OBJS) \
19041912
$(VCSSVN_OBJS) \
1913+
common-main.o \
19051914
git.o
19061915
ifndef NO_CURL
19071916
OBJECTS += http.o http-walker.o remote-curl.o

common-main.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "cache.h"
2+
#include "exec_cmd.h"
3+
4+
/*
5+
* Many parts of Git have subprograms communicate via pipe, expect the
6+
* upstream of a pipe to die with SIGPIPE when the downstream of a
7+
* pipe does not need to read all that is written. Some third-party
8+
* programs that ignore or block SIGPIPE for their own reason forget
9+
* to restore SIGPIPE handling to the default before spawning Git and
10+
* break this carefully orchestrated machinery.
11+
*
12+
* Restore the way SIGPIPE is handled to default, which is what we
13+
* expect.
14+
*/
15+
static void restore_sigpipe_to_default(void)
16+
{
17+
sigset_t unblock;
18+
19+
sigemptyset(&unblock);
20+
sigaddset(&unblock, SIGPIPE);
21+
sigprocmask(SIG_UNBLOCK, &unblock, NULL);
22+
signal(SIGPIPE, SIG_DFL);
23+
}
24+
25+
int main(int argc, const char **argv)
26+
{
27+
/*
28+
* Always open file descriptors 0/1/2 to avoid clobbering files
29+
* in die(). It also avoids messing up when the pipes are dup'ed
30+
* onto stdin/stdout/stderr in the child processes we spawn.
31+
*/
32+
sanitize_stdfds();
33+
34+
git_setup_gettext();
35+
36+
argv[0] = git_extract_argv0_path(argv[0]);
37+
38+
restore_sigpipe_to_default();
39+
40+
return cmd_main(argc, argv);
41+
}

compat/mingw.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ extern CRITICAL_SECTION pinfo_cs;
535535
void mingw_startup();
536536
#define main(c,v) dummy_decl_mingw_main(); \
537537
static int mingw_main(c,v); \
538-
int main(int argc, char **argv) \
538+
int main(int argc, const char **argv) \
539539
{ \
540540
mingw_startup(); \
541541
return mingw_main(__argc, (void *)__argv); \

credential-cache--daemon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ static void init_socket_directory(const char *path)
257257
free(path_copy);
258258
}
259259

260-
int main(int argc, const char **argv)
260+
int cmd_main(int argc, const char **argv)
261261
{
262262
const char *socket_path;
263263
int ignore_sighup = 0;

credential-cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static void do_cache(const char *socket, const char *action, int timeout,
8383
strbuf_release(&buf);
8484
}
8585

86-
int main(int argc, const char **argv)
86+
int cmd_main(int argc, const char **argv)
8787
{
8888
char *socket_path = NULL;
8989
int timeout = 900;

credential-store.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ static void lookup_credential(const struct string_list *fns, struct credential *
142142
return; /* Found credential */
143143
}
144144

145-
int main(int argc, char **argv)
145+
int cmd_main(int argc, const char **argv)
146146
{
147147
const char * const usage[] = {
148148
"git credential-store [<options>] <action>",

daemon.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "cache.h"
22
#include "pkt-line.h"
3-
#include "exec_cmd.h"
43
#include "run-command.h"
54
#include "strbuf.h"
65
#include "string-list.h"
@@ -32,7 +31,7 @@ static const char daemon_usage[] =
3231
" [<directory>...]";
3332

3433
/* List of acceptable pathname prefixes */
35-
static char **ok_paths;
34+
static const char **ok_paths;
3635
static int strict_paths;
3736

3837
/* If this is set, git-daemon-export-ok is not required */
@@ -240,7 +239,7 @@ static const char *path_ok(const char *directory, struct hostinfo *hi)
240239
}
241240

242241
if ( ok_paths && *ok_paths ) {
243-
char **pp;
242+
const char **pp;
244243
int pathlen = strlen(path);
245244

246245
/* The validation is done on the paths after enter_repo
@@ -1178,7 +1177,7 @@ static int serve(struct string_list *listen_addr, int listen_port,
11781177
return service_loop(&socklist);
11791178
}
11801179

1181-
int main(int argc, char **argv)
1180+
int cmd_main(int argc, const char **argv)
11821181
{
11831182
int listen_port = 0;
11841183
struct string_list listen_addr = STRING_LIST_INIT_NODUP;
@@ -1188,12 +1187,8 @@ int main(int argc, char **argv)
11881187
struct credentials *cred = NULL;
11891188
int i;
11901189

1191-
git_setup_gettext();
1192-
1193-
git_extract_argv0_path(argv[0]);
1194-
11951190
for (i = 1; i < argc; i++) {
1196-
char *arg = argv[i];
1191+
const char *arg = argv[i];
11971192
const char *v;
11981193

11991194
if (skip_prefix(arg, "--listen=", &v)) {
@@ -1367,8 +1362,7 @@ int main(int argc, char **argv)
13671362
if (detach) {
13681363
if (daemonize())
13691364
die("--detach not supported on this platform");
1370-
} else
1371-
sanitize_stdfds();
1365+
}
13721366

13731367
if (pid_file)
13741368
write_file(pid_file, "%"PRIuMAX, (uintmax_t) getpid());

fast-import.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ Format of STDIN stream:
164164
#include "refs.h"
165165
#include "csum-file.h"
166166
#include "quote.h"
167-
#include "exec_cmd.h"
168167
#include "dir.h"
169168

170169
#define PACK_ID_BITS 16
@@ -300,7 +299,7 @@ static int failure;
300299
static FILE *pack_edges;
301300
static unsigned int show_stats = 1;
302301
static int global_argc;
303-
static char **global_argv;
302+
static const char **global_argv;
304303

305304
/* Memory pools */
306305
static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
@@ -3384,14 +3383,10 @@ static void parse_argv(void)
33843383
read_marks();
33853384
}
33863385

3387-
int main(int argc, char **argv)
3386+
int cmd_main(int argc, const char **argv)
33883387
{
33893388
unsigned int i;
33903389

3391-
git_extract_argv0_path(argv[0]);
3392-
3393-
git_setup_gettext();
3394-
33953390
if (argc == 2 && !strcmp(argv[1], "-h"))
33963391
usage(fast_import_usage);
33973392

git-compat-util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,3 +1045,5 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);
10451045
#endif
10461046

10471047
#endif
1048+
1049+
extern int cmd_main(int, const char **);

git.c

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -609,48 +609,15 @@ static int run_argv(int *argcp, const char ***argv)
609609
return done_alias;
610610
}
611611

612-
/*
613-
* Many parts of Git have subprograms communicate via pipe, expect the
614-
* upstream of a pipe to die with SIGPIPE when the downstream of a
615-
* pipe does not need to read all that is written. Some third-party
616-
* programs that ignore or block SIGPIPE for their own reason forget
617-
* to restore SIGPIPE handling to the default before spawning Git and
618-
* break this carefully orchestrated machinery.
619-
*
620-
* Restore the way SIGPIPE is handled to default, which is what we
621-
* expect.
622-
*/
623-
static void restore_sigpipe_to_default(void)
624-
{
625-
sigset_t unblock;
626-
627-
sigemptyset(&unblock);
628-
sigaddset(&unblock, SIGPIPE);
629-
sigprocmask(SIG_UNBLOCK, &unblock, NULL);
630-
signal(SIGPIPE, SIG_DFL);
631-
}
632-
633-
int main(int argc, char **av)
612+
int cmd_main(int argc, const char **argv)
634613
{
635-
const char **argv = (const char **) av;
636614
const char *cmd;
637615
int done_help = 0;
638616

639-
cmd = git_extract_argv0_path(argv[0]);
617+
cmd = argv[0];
640618
if (!cmd)
641619
cmd = "git-help";
642620

643-
/*
644-
* Always open file descriptors 0/1/2 to avoid clobbering files
645-
* in die(). It also avoids messing up when the pipes are dup'ed
646-
* onto stdin/stdout/stderr in the child processes we spawn.
647-
*/
648-
sanitize_stdfds();
649-
650-
restore_sigpipe_to_default();
651-
652-
git_setup_gettext();
653-
654621
trace_command_performance(argv);
655622

656623
/*

0 commit comments

Comments
 (0)