CheckAccessDeleted::findService interface for testing.
[platform/upstream/libzypp.git] / zypp / misc / CheckAccessDeleted.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/misc/CheckAccessDeleted.h
10  *
11 */
12 #ifndef ZYPP_MISC_CHECKACCESSDELETED_H
13 #define ZYPP_MISC_CHECKACCESSDELETED_H
14
15 #include <iosfwd>
16 #include <vector>
17 #include <string>
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22
23   /**
24    * Check for running processes which access deleted files or libraries.
25    *
26    * Executed after commit, this gives a hint which processes/services
27    * need to be restarted.
28    *
29    * Per default upon construtcion or explicit call to \ref check,
30    * information about running processes which access deleted files
31    * or libraries is collected and provided as a \ref ProcInfo
32    * container.
33    */
34   class CheckAccessDeleted
35   {
36     public:
37       /**
38        * Data about one running process accessing deleted files.
39        */
40       struct ProcInfo
41       {
42         std::string pid;                //!< process ID
43         std::string ppid;               //!< parent process ID
44         std::string puid;               //!< process user ID
45         std::string login;              //!< process login name
46         std::string command;            //!< process command name
47         std::vector<std::string> files; //!< list of deleted files or libraries accessed
48
49         /** Guess if command was started by an \c /etc/init.d/ script.
50          * The name of an \c /etc/init.d/ script that might be used to restart the
51          * command.
52          * \warning This is just a guess.
53         */
54         std::string service() const;
55       };
56
57       typedef size_t                                    size_type;
58       typedef ProcInfo                                  value_type;
59       typedef std::vector<ProcInfo>::const_iterator     const_iterator;
60
61     public:
62       /** Default ctor performs check immediately.
63        * Pass \c false and the initial check is omitted.
64        * \throws Exception if \ref check throws.
65        * \see \ref check.
66        */
67       CheckAccessDeleted( bool doCheck_r = true )
68       { if ( doCheck_r ) check(); }
69
70     public:
71       /** Check for running processes which access deleted files or libraries.
72        * \return the number of processes found.
73        * \throws Exception On error collecting the data (e.g. no lsof installed)
74        */
75       size_type check();
76
77       bool empty() const                { return _data.empty(); }
78       size_type size() const            { return _data.size(); }
79       const_iterator begin() const      { return _data.begin(); }
80       const_iterator end() const        { return _data.end(); }
81
82     public:
83       /** Guess if \c command was started by an \c /etc/init.d/ script.
84        * The name of an \c /etc/init.d/ script that might be used to restart the
85        * command. \c command may be specifies by name, full path or pid.
86        * \warning This is just a guess.
87        */
88       static std::string findService( const char * command_r );
89       /** \overload Taking a string.*/
90       static std::string findService( const std::string & command_r );
91       /** \overload Taking a pathname. */
92       static std::string findService( const Pathname & command_r );
93       /** \overload taking the pid. */
94       static std::string findService( pid_t pid_r );
95
96     private:
97       std::vector<ProcInfo> _data;
98   };
99   ///////////////////////////////////////////////////////////////////
100
101   /** \relates CheckAccessDeleted Stream output */
102   std::ostream & operator<<( std::ostream & str, const CheckAccessDeleted & obj );
103
104   /** \relates CheckAccessDeleted::ProcInfo Stream output */
105   std::ostream & operator<<( std::ostream & str, const CheckAccessDeleted::ProcInfo & obj );
106
107   /////////////////////////////////////////////////////////////////
108 } // namespace zypp
109 ///////////////////////////////////////////////////////////////////
110 #endif // ZYPP_MISC_CHECKACCESSDELETED_H