| 1161 | } |
| 1162 | |
| 1163 | int start_async(struct async *async) |
| 1164 | { |
| 1165 | int need_in, need_out; |
| 1166 | int fdin[2], fdout[2]; |
| 1167 | int proc_in, proc_out; |
| 1168 | |
| 1169 | need_in = async->in < 0; |
| 1170 | if (need_in) { |
| 1171 | if (pipe(fdin) < 0) { |
| 1172 | if (async->out > 0) |
| 1173 | close(async->out); |
| 1174 | return error_errno("cannot create pipe"); |
| 1175 | } |
| 1176 | async->in = fdin[1]; |
| 1177 | } |
| 1178 | |
| 1179 | need_out = async->out < 0; |
| 1180 | if (need_out) { |
| 1181 | if (pipe(fdout) < 0) { |
| 1182 | if (need_in) |
| 1183 | close_pair(fdin); |
| 1184 | else if (async->in) |
| 1185 | close(async->in); |
| 1186 | return error_errno("cannot create pipe"); |
| 1187 | } |
| 1188 | async->out = fdout[0]; |
| 1189 | } |
| 1190 | |
| 1191 | if (need_in) |
| 1192 | proc_in = fdin[0]; |
| 1193 | else if (async->in) |
| 1194 | proc_in = async->in; |
| 1195 | else |
| 1196 | proc_in = -1; |
| 1197 | |
| 1198 | if (need_out) |
| 1199 | proc_out = fdout[1]; |
| 1200 | else if (async->out) |
| 1201 | proc_out = async->out; |
| 1202 | else |
| 1203 | proc_out = -1; |
| 1204 | |
| 1205 | #ifdef NO_PTHREADS |
| 1206 | /* Flush stdio before fork() to avoid cloning buffers */ |
| 1207 | fflush(NULL); |
| 1208 | |
| 1209 | async->pid = fork(); |
| 1210 | if (async->pid < 0) { |
| 1211 | error_errno("fork (async) failed"); |
| 1212 | goto error; |
| 1213 | } |
| 1214 | if (!async->pid) { |
| 1215 | if (need_in) |
| 1216 | close(fdin[1]); |
| 1217 | if (need_out) |
| 1218 | close(fdout[0]); |
| 1219 | git_atexit_clear(); |
| 1220 | process_is_async = 1; |
no test coverage detected