Add class CheckAccessDeleted and command zypp-CheckAccessDeleted.
[platform/upstream/libzypp.git] / tools / zypp-CheckAccessDeleted.cc
1 #include <iostream>
2 #include <zypp/base/Easy.h>
3 #include <zypp/base/String.h>
4 #include <zypp/base/Exception.h>
5 #include <zypp/Pathname.h>
6 #include <zypp/misc/CheckAccessDeleted.h>
7
8 /** Collect max string size. */
9 struct TableCol
10 {
11   TableCol( size_t size_r = 0 )
12   : size( size_r )
13   {}
14   TableCol( const std::string & header_r )
15   : header( header_r ), size( header_r.size() )
16   {}
17   void operator()( const std::string & val_r )
18   { if ( val_r.size() > size ) size = val_r.size(); }
19
20   std::string header;
21   size_t      size;
22 };
23
24 /** Scan to determine column sizes, then print. */
25 struct ProcInfoTable
26 {
27   ProcInfoTable()
28   : pid         ( "PID" )
29   , ppid        ( "PPID" )
30   , puid        ( "UID" )
31   , login       ( "LOGIN" )
32   , command     ( "COMMAND" )
33   , service     ( "SERVICE" )
34   , files       ( "FILES" )
35   {}
36
37   void scan( const zypp::CheckAccessDeleted::ProcInfo & val_r )
38   {
39     pid( val_r.pid );
40     ppid( val_r.ppid );
41     puid( val_r.puid );
42     login( val_r.login );
43     command( val_r.command );
44     service( val_r.service() );
45   }
46
47   void printHeader() const
48   {
49     printRow( pid.header,
50               ppid.header,
51               puid.header,
52               login.header,
53               command.header,
54               service.header,
55               files.header );
56   }
57
58   void print( const zypp::CheckAccessDeleted::ProcInfo & val_r ) const
59   {
60     printRow( val_r.pid,
61               val_r.ppid,
62               val_r.puid,
63               val_r.login,
64               val_r.command,
65               val_r.service(),
66               zypp::str::join( val_r.files, ", " ) );
67   }
68
69   void printRow( const std::string & pid_r,
70                  const std::string & ppid_r,
71                  const std::string & puid_r,
72                  const std::string & login_r,
73                  const std::string & command_r,
74                  const std::string & service_r,
75                  const std::string & files_r ) const
76   {
77     printf( "%*s %*s %*s  %-*s %-*s %-*s %-s\n",
78             pid.size, pid_r.c_str(),
79             ppid.size, ppid_r.c_str(),
80             puid.size, puid_r.c_str(),
81             login.size, login_r.c_str(),
82             command.size, command_r.c_str(),
83             service.size, (service_r.empty() ? " -" : service_r.c_str()),
84             files_r.c_str() );
85   }
86
87   TableCol pid;
88   TableCol ppid;
89   TableCol puid;
90   TableCol login;
91   TableCol command;
92   TableCol service;
93   TableCol files;
94 };
95
96 int main( int argc, char * argv[] )
97 {
98   if ( argc >= 2 )
99   {
100     std::string progname( zypp::Pathname::basename( argv[0] ) );
101     if ( strcmp( argv[1], "--help" ) == 0 )
102     {
103       std::cout << "Usage: " << progname << " [--help]" << std::endl;
104       std::cout << "List information about all running processe" << std::endl;
105       std::cout << "which access deleted files or libraries." << std::endl;
106       std::cout << "  PID     " << "process ID" << std::endl;
107       std::cout << "  PPID    " << "parent process ID" << std::endl;
108       std::cout << "  UID     " << "process user ID" << std::endl;
109       std::cout << "  LOGIN   " << "process login name" << std::endl;
110       std::cout << "  COMMAND " << "process command name" << std::endl;
111       std::cout << "  SERVICE " << "/etc/init.d/ script that might be used to restart the command (guessed)" << std::endl;
112       std::cout << "  FILES   " << "list of deleted files or libraries accessed" << std::endl;
113       return 0;
114     }
115     std::cerr << progname << ": unexpected argument '" << argv[1] << "'" << std::endl;
116     std::cerr << "Try `" << progname << " --help' for more information." << std::endl;
117     return 1;
118   }
119
120   zypp::CheckAccessDeleted checker(false); // wait for explicit call to check()
121   try {
122     checker.check();
123   }
124   catch( const zypp::Exception & err )
125   {
126     std::cerr << err << std::endl << err.historyAsString();
127     return 2;
128   }
129
130   ProcInfoTable table;
131   for_( it, checker.begin(), checker.end() )
132     table.scan( *it );
133
134   table.printHeader();
135   for_( it, checker.begin(), checker.end() )
136     table.print( *it );
137
138   return 0;
139 }