2 * Generic implementation of background process infrastructure.
4 #include "git-compat-util.h"
5 #include "sub-process.h"
9 int cmd2process_cmp(const void *cmp_data UNUSED
,
10 const struct hashmap_entry
*eptr
,
11 const struct hashmap_entry
*entry_or_key
,
12 const void *keydata UNUSED
)
14 const struct subprocess_entry
*e1
, *e2
;
16 e1
= container_of(eptr
, const struct subprocess_entry
, ent
);
17 e2
= container_of(entry_or_key
, const struct subprocess_entry
, ent
);
19 return strcmp(e1
->cmd
, e2
->cmd
);
22 struct subprocess_entry
*subprocess_find_entry(struct hashmap
*hashmap
, const char *cmd
)
24 struct subprocess_entry key
;
26 hashmap_entry_init(&key
.ent
, strhash(cmd
));
28 return hashmap_get_entry(hashmap
, &key
, ent
, NULL
);
31 int subprocess_read_status(int fd
, struct strbuf
*status
)
38 len
= packet_read_line_gently(fd
, NULL
, &line
);
39 if ((len
< 0) || !line
)
41 pair
= strbuf_split_str(line
, '=', 2);
42 if (pair
[0] && pair
[0]->len
&& pair
[1]) {
43 /* the last "status=<foo>" line wins */
44 if (!strcmp(pair
[0]->buf
, "status=")) {
46 strbuf_addbuf(status
, pair
[1]);
49 strbuf_list_free(pair
);
52 return (len
< 0) ? len
: 0;
55 void subprocess_stop(struct hashmap
*hashmap
, struct subprocess_entry
*entry
)
60 entry
->process
.clean_on_exit
= 0;
61 kill(entry
->process
.pid
, SIGTERM
);
62 finish_command(&entry
->process
);
64 hashmap_remove(hashmap
, &entry
->ent
, NULL
);
67 static void subprocess_exit_handler(struct child_process
*process
)
69 sigchain_push(SIGPIPE
, SIG_IGN
);
70 /* Closing the pipe signals the subprocess to initiate a shutdown. */
73 sigchain_pop(SIGPIPE
);
74 /* Finish command will wait until the shutdown is complete. */
75 finish_command(process
);
78 int subprocess_start(struct hashmap
*hashmap
, struct subprocess_entry
*entry
, const char *cmd
,
79 subprocess_start_fn startfn
)
82 struct child_process
*process
;
85 process
= &entry
->process
;
87 child_process_init(process
);
88 strvec_push(&process
->args
, cmd
);
89 process
->use_shell
= 1;
92 process
->clean_on_exit
= 1;
93 process
->clean_on_exit_handler
= subprocess_exit_handler
;
94 process
->trace2_child_class
= "subprocess";
96 err
= start_command(process
);
98 error("cannot fork to run subprocess '%s'", cmd
);
102 hashmap_entry_init(&entry
->ent
, strhash(cmd
));
104 err
= startfn(entry
);
106 error("initialization for subprocess '%s' failed", cmd
);
107 subprocess_stop(hashmap
, entry
);
111 hashmap_add(hashmap
, &entry
->ent
);
115 static int handshake_version(struct child_process
*process
,
116 const char *welcome_prefix
, int *versions
,
125 chosen_version
= &version_scratch
;
127 if (packet_write_fmt_gently(process
->in
, "%s-client\n",
129 return error("Could not write client identification");
130 for (i
= 0; versions
[i
]; i
++) {
131 if (packet_write_fmt_gently(process
->in
, "version=%d\n",
133 return error("Could not write requested version");
135 if (packet_flush_gently(process
->in
))
136 return error("Could not write flush packet");
138 if (!(line
= packet_read_line(process
->out
, NULL
)) ||
139 !skip_prefix(line
, welcome_prefix
, &p
) ||
140 strcmp(p
, "-server"))
141 return error("Unexpected line '%s', expected %s-server",
142 line
? line
: "<flush packet>", welcome_prefix
);
143 if (!(line
= packet_read_line(process
->out
, NULL
)) ||
144 !skip_prefix(line
, "version=", &p
) ||
145 strtol_i(p
, 10, chosen_version
))
146 return error("Unexpected line '%s', expected version",
147 line
? line
: "<flush packet>");
148 if ((line
= packet_read_line(process
->out
, NULL
)))
149 return error("Unexpected line '%s', expected flush", line
);
151 /* Check to make sure that the version received is supported */
152 for (i
= 0; versions
[i
]; i
++) {
153 if (versions
[i
] == *chosen_version
)
157 return error("Version %d not supported", *chosen_version
);
162 static int handshake_capabilities(struct child_process
*process
,
163 struct subprocess_capability
*capabilities
,
164 unsigned int *supported_capabilities
)
169 for (i
= 0; capabilities
[i
].name
; i
++) {
170 if (packet_write_fmt_gently(process
->in
, "capability=%s\n",
171 capabilities
[i
].name
))
172 return error("Could not write requested capability");
174 if (packet_flush_gently(process
->in
))
175 return error("Could not write flush packet");
177 while ((line
= packet_read_line(process
->out
, NULL
))) {
179 if (!skip_prefix(line
, "capability=", &p
))
183 capabilities
[i
].name
&& strcmp(p
, capabilities
[i
].name
);
186 if (capabilities
[i
].name
) {
187 if (supported_capabilities
)
188 *supported_capabilities
|= capabilities
[i
].flag
;
190 die("subprocess '%s' requested unsupported capability '%s'",
191 process
->args
.v
[0], p
);
198 int subprocess_handshake(struct subprocess_entry
*entry
,
199 const char *welcome_prefix
,
202 struct subprocess_capability
*capabilities
,
203 unsigned int *supported_capabilities
)
206 struct child_process
*process
= &entry
->process
;
208 sigchain_push(SIGPIPE
, SIG_IGN
);
210 retval
= handshake_version(process
, welcome_prefix
, versions
,
212 handshake_capabilities(process
, capabilities
,
213 supported_capabilities
);
215 sigchain_pop(SIGPIPE
);