summaryrefslogtreecommitdiff
path: root/db/curop.h
blob: 2ba4f90d0579d0242ad4563265723fcfd681aeb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// curop.h

#pragma once

#include "namespace.h"
#include "security.h"

namespace mongo { 

    extern struct CurOp {
        void reset(time_t now, const sockaddr_in &_client) { 
            active = true;
            opNum++;
            startTime = now;
            ns[0] = '?'; // just in case not set later
            *query = 0;
            killCurrentOp = 0;
            client = _client;
        }

        bool active;
        unsigned opNum;
        time_t startTime;
        int op;
        char ns[Namespace::MaxNsLen+2];
        char query[128];
        char zero;
        struct sockaddr_in client;

        CurOp() { 
            opNum = 0; 
            // These addresses should never be written to again.  The zeroes are
            // placed here as a precaution because currentOp may be accessed
            // without the db mutex.
            ns[sizeof(ns)-1] = 0;
            query[sizeof(query)-1] = 0;
        }

        BSONObj info() { 
            AuthenticationInfo *ai = authInfo.get();
            if( ai == 0 || !ai->isAuthorized("admin") ) { 
                BSONObjBuilder b;
                b.append("err", "unauthorized");
                return b.obj();
            }
            return infoNoauth();
        }
        
        BSONObj infoNoauth() {
            BSONObjBuilder b;
            b.append("opid", opNum);
            b.append("active", active);
            if( active ) 
                b.append("secs_running", (int) (time(0)-startTime));
            if( op == 2004 ) 
                b.append("op", "query");
            else if( op == 2005 )
                b.append("op", "getMore");
            else if( op == 2001 )
                b.append("op", "update");
            else if( op == 2002 )
                b.append("op", "insert");
            else if( op == 2006 )
                b.append("op", "delete");
            else
                b.append("op", op);
            b.append("ns", ns);
            b.append("query", query);
            b.append("inLock",  dbMutexInfo.isLocked());
            stringstream clientStr;
            clientStr << inet_ntoa( client.sin_addr ) << ":" << ntohs( client.sin_port );
            b.append("client", clientStr.str());
            return b.obj();
        }
    } currentOp;

}