locktests.c revision 72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7
/* liblxcapi
*
* Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
* Copyright © 2012 Canonical Ltd.
*
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#define mycontainername "lxctest.sem"
#define TIMEOUT_SECS 3
int timedout;
int pid_to_kill;
void timeouthandler(int sig)
{
// timeout received
timedout = 1;
}
void starttimer(int secs)
{
timedout = 0;
}
void stoptimer(void)
{
alarm(0);
}
{
int ret;
stoptimer();
if (ret == 0) {
return 0;
}
if (timedout)
else
return 1;
}
/*
* get one lock. Fork a second task to try to get a second lock,
* with infinite timeout. If our alarm hits, kill the second
* task. If second task does not
*/
{
int status;
int ret;
if (ret) {
return 2;
}
pid_to_kill = fork();
if (pid_to_kill < 0) {
return 3;
}
if (pid_to_kill == 0) { // child
if (ret == 0) {
exit(0);
}
exit(1);
}
stoptimer();
// child exited normally - timeout didn't kill it
if (WEXITSTATUS(status) == 0)
else
return 1;
}
return 0;
}
/*
* get one lock. try to get second lock, but asking for timeout. If
* should return failure. If our own alarm, set at twice the lock
* request's timeout, hits, then lxclock() did not properly time out.
*/
{
int status;
int ret = 0;
if (ret) {
return 2;
}
pid_to_kill = fork();
if (pid_to_kill < 0) {
return 2;
}
if (pid_to_kill == 0) {
if (ret == 0) {
exit(0);
}
exit(1);
}
stoptimer();
return 1;
}
if (WEXITSTATUS(status) == 0) {
ret = 1;
}
return ret;
}
{
if (!lock) {
exit(1);
}
if (ret) {
exit(1);
}
if (ret) {
exit(1);
}
if (!lock) {
exit(1);
}
if (!r) {
} else {
}
if (ret) {
goto out;
}
if (!r) {
} else {
}
if (ret) {
goto out;
}
if (!r) {
} else {
}
if (ret) {
goto out;
}
if (!r) {
} else {
}
out:
}