dup2-array.c revision d7bd88416567f07b56581a65e1cd46a42adb79c0
/* Copyright (c) 2007 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "fd-close-on-exec.h"
#include "dup2-array.h"
#include <unistd.h>
{
struct dup2 d;
}
{
int fd;
t_push();
for (;;) {
for (i = 0; i < count; i++) {
if (moved[i])
continue;
for (j = 0; j < count; j++) {
!moved[j]) {
conflict = j;
break;
}
}
if (j == count) {
/* no conflicts, move it */
i_error("dup2(%d, %d) failed: %m",
t_pop();
return -1;
}
}
}
break;
if (moves) {
/* it's possible that the conflicting fd was
moved already. try again. */
continue;
}
/* ok, we have to dup() */
if (fd == -1) {
t_pop();
return -1;
}
}
t_pop();
return 0;
}