1 /*  $Id: test_ncbi_os_unix.cpp 633579 2021-06-22 14:44:34Z ivanov $
2  * ===========================================================================
3  *
4  *                            PUBLIC DOMAIN NOTICE
5  *               National Center for Biotechnology Information
6  *
7  *  This software/database is a "United States Government Work" under the
8  *  terms of the United States Copyright Act.  It was written as part of
9  *  the author's official duties as a United States Government employee and
10  *  thus cannot be copyrighted.  This software/database is freely available
11  *  to the public for use. The National Library of Medicine and the U.S.
12  *  Government have not placed any restriction on its use or reproduction.
13  *
14  *  Although all reasonable efforts have been taken to ensure the accuracy
15  *  and reliability of the software and data, the NLM and the U.S.
16  *  Government do not and cannot warrant the performance or results that
17  *  may be obtained by using this software or data. The NLM and the U.S.
18  *  Government disclaim all warranties, express or implied, including
19  *  warranties of performance, merchantability or fitness for any particular
20  *  purpose.
21  *
22  *  Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author:  Anton Lavrentiev
27  *
28  */
29 
30 #include <ncbi_pch.hpp>
31 #include <corelib/ncbidiag.hpp>
32 #include <corelib/ncbithr.hpp>
33 #include <corelib/ncbi_process.hpp>
34 #include <corelib/ncbi_system.hpp>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <common/test_assert.h>  /* This header must go last */
39 
40 USING_NCBI_SCOPE;
41 
42 #ifdef NCBI_THREADS
43 struct SChildThread : CThread
44 {
45 protected:
MainSChildThread46     void* Main(void) { SleepSec(10); return NULL; }
47 };
48 #endif
49 
main()50 int main()
51 {
52     // Set err.-posting and tracing to maximum
53     SetDiagTrace(eDT_Enable);
54     SetDiagPostFlag(eDPF_All);
55     SetDiagPostLevel(eDiag_Info);
56 
57     // Run tests
58 #ifdef NCBI_THREADS
59     LOG_POST("Trying to daemonize while running a child thread, expecting failure");
60     SChildThread* child = new SChildThread;
61     child->Run();
62     _ASSERT(CCurrentProcess::Daemonize() == 0);
63     child->Join();
64 #endif
65 
66     LOG_POST("Trying to daemonize at \"/\", "
67              "expecting failure for the check to continue successfully");
68     _ASSERT(CCurrentProcess::Daemonize("/test_ncbi_os_unix.log") == 0);
69     _ASSERT(errno == EACCES  ||  errno == EPERM  ||  errno == ENOENT
70             ||  errno == EROFS/*Darwin seems to have that in place*/);
71     _ASSERT(CCurrentProcess::Daemonize("./test_ncbi_os_unix.log",
72                                        CCurrentProcess::fDF_KeepCWD |
73                                        CCurrentProcess::fDF_KeepStdout) != 0);
74 
75     LOG_POST("Trying to daemonize at current location, expecting success");
76     _ASSERT(access("./test_ncbi_os_unix.log", F_OK) == 0);
77 
78     LOG_POST("TEST COMPLETED SUCCESSFULLY");
79     sleep(60);
80     return 0;
81 }
82