Imported Upstream version 14.45.0
[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 executables 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
37     public:
38       /**
39        * Data about one running process accessing deleted files.
40        */
41       struct ProcInfo
42       {
43         std::string pid;                //!< process ID
44         std::string ppid;               //!< parent process ID
45         std::string puid;               //!< process user ID
46         std::string login;              //!< process login name
47         std::string command;            //!< process command name
48         std::vector<std::string> files; //!< list of deleted executables or libraries accessed
49
50         /** Guess if command was started by an \c /etc/init.d/ script.
51          * The name of an \c /etc/init.d/ script that might be used to restart the
52          * command.
53          * \warning This is just a guess.
54         */
55         std::string service() const;
56       };
57
58       typedef size_t                                    size_type;
59       typedef ProcInfo                                  value_type;
60       typedef std::vector<ProcInfo>::const_iterator     const_iterator;
61
62     public:
63       /** Default ctor performs check immediately.
64        * Pass \c false and the initial check is omitted.
65        * \throws Exception if \ref check throws.
66        * \see \ref check.
67        */
68       CheckAccessDeleted( bool doCheck_r = true )
69       { if ( doCheck_r ) check(); }
70
71     public:
72       /** Check for running processes which access deleted executables or libraries.
73        *
74        * Per default \ref check will try guess and collect executables and
75        * libraries only by looking at the files path and name. (e.g named
76        * \c lib* or located in \c *bin/).
77        *
78        * A verbose check will omit this test and collect all processes uning
79        * any deleted file.
80        *
81        * \return the number of processes found.
82        * \throws Exception On error collecting the data (e.g. no lsof installed)
83        */
84       size_type check( bool verbose_r = false );
85
86       bool empty() const                { return _data.empty(); }
87       size_type size() const            { return _data.size(); }
88       const_iterator begin() const      { return _data.begin(); }
89       const_iterator end() const        { return _data.end(); }
90
91     public:
92       /** Guess if \c command was started by an \c /etc/init.d/ script.
93        * The name of an \c /etc/init.d/ script that might be used to restart the
94        * command. \c command may be specifies by name, full path or pid.
95        * \warning This is just a guess.
96        */
97       static std::string findService( const char * command_r );
98       /** \overload Taking a string.*/
99       static std::string findService( const std::string & command_r );
100       /** \overload Taking a pathname. */
101       static std::string findService( const Pathname & command_r );
102       /** \overload taking the pid. */
103       static std::string findService( pid_t pid_r );
104
105     private:
106       std::vector<ProcInfo> _data;
107   };
108   ///////////////////////////////////////////////////////////////////
109
110   /** \relates CheckAccessDeleted Stream output */
111   std::ostream & operator<<( std::ostream & str, const CheckAccessDeleted & obj );
112
113   /** \relates CheckAccessDeleted::ProcInfo Stream output */
114   std::ostream & operator<<( std::ostream & str, const CheckAccessDeleted::ProcInfo & obj );
115
116   /////////////////////////////////////////////////////////////////
117 } // namespace zypp
118 ///////////////////////////////////////////////////////////////////
119 #endif // ZYPP_MISC_CHECKACCESSDELETED_H