opnsense-src/lib/libutil++/tests/pidfile_test.cc
John Baldwin 7be913e00d libutil++: Add freebsd::pidfile wrapper class around struct pidfh
This class wraps the pidfile_* API from libutil.  The destructor calls
pidfile_remove() when an object is destroyed.  This class is similar
to std::unique_ptr<> in that it retains exclusive ownership of the
pidfh object.

In addition to release and reset methods, write, close, and fileno
methods are provided as wrappers for pidfile_*.

Sponsored by:	Chelsio Communications
Pull Request:	https://github.com/freebsd/freebsd-src/pull/1794
2025-08-04 15:38:07 -04:00

44 lines
949 B
C++

/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2025 Chelsio Communications, Inc.
* Written by: John Baldwin <jhb@FreeBSD.org>
*/
#include <atf-c++.hpp>
#include <sys/stat.h>
#include <libutil.h>
#include <libutil++.hh>
ATF_TEST_CASE_WITHOUT_HEAD(basic);
ATF_TEST_CASE_BODY(basic)
{
pid_t other;
struct pidfh *pfh = pidfile_open("test_pidfile", 0600, &other);
ATF_REQUIRE(pfh != nullptr);
ATF_REQUIRE(pidfile_fileno(pfh) >= 0);
struct stat sb;
ATF_REQUIRE(fstat(pidfile_fileno(pfh), &sb) == 0);
ATF_REQUIRE_EQ(0, sb.st_size);
freebsd::pidfile pf(pfh);
ATF_REQUIRE_EQ(pidfile_fileno(pfh), pf.fileno());
ATF_REQUIRE(pf.write() == 0);
ATF_REQUIRE(fstat(pf.fileno(), &sb) == 0);
ATF_REQUIRE(sb.st_size > 0);
ATF_REQUIRE(pf.close() == 0);
ATF_REQUIRE(pf.fileno() == -1);
ATF_REQUIRE_EQ(EDOOFUS, errno);
ATF_REQUIRE(unlink("test_pidfile") == 0);
}
ATF_INIT_TEST_CASES(tcs)
{
ATF_ADD_TEST_CASE(tcs, basic);
}