test-async.c revision e6b5c5d03cb28d2149dc1c124c2a315911b91f4f
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier/***
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier This file is part of systemd
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier Copyright 2014 Ronny Chevalier
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier systemd is free software; you can redistribute it and/or modify it
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier under the terms of the GNU Lesser General Public License as published by
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier the Free Software Foundation; either version 2.1 of the License, or
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier (at your option) any later version.
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier systemd is distributed in the hope that it will be useful, but
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier WITHOUT ANY WARRANTY; without even the implied warranty of
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier Lesser General Public License for more details.
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier You should have received a copy of the GNU Lesser General Public License
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier along with systemd; If not, see <http://www.gnu.org/licenses/>.
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier***/
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier#include <unistd.h>
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier#include "async.h"
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier#include "util.h"
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier#include "macro.h"
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalierstatic bool test_async = false;
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalierstatic void *async_func(void *arg) {
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier test_async = true;
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier return NULL;
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier}
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalierint main(int argc, char *argv[]) {
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier int fd;
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier char name[] = "/tmp/test-asynchronous_close.XXXXXX";
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier assert_se(fd >= 0);
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier asynchronous_close(fd);
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier assert_se(asynchronous_job(async_func, NULL) >= 0);
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier assert_se(asynchronous_sync() >= 0);
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier sleep(1);
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier assert_se(fcntl(fd, F_GETFD) == -1);
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier assert_se(test_async);
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier return 0;
e6b5c5d03cb28d2149dc1c124c2a315911b91f4fRonny Chevalier}