util_lock.c revision e9086c9178b06ff396b958dcc2a1e5baa0693f5c
/*
SSSD
Authors:
Michal Zidek <mzidek@redhat.com>
Copyright (C) 2012 Red Hat
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
{
int ret;
int retries_left;
if (num_tries <= 0) {
return EINVAL;
}
if (ret == -1) {
("Failed to lock file. Retries left: %d\n",
retries_left - 1));
/* File is locked by someone else. Return EACCESS
* if this is the last try. */
return EACCES;
}
if (retries_left - 1 > 0) {
if (ret == -1) {
("usleep() failed -> ignoring\n"));
}
}
} else {
/* Error occurred */
("Unable to lock file.\n"));
return ret;
}
} else if (ret == 0) {
/* File successfuly locked */
break;
}
}
if (retries_left == 0) {
return ret;
}
return EOK;
}