Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ab71ef08 authored by Ruchir Rastogi's avatar Ruchir Rastogi
Browse files

Store client pid within LogEvent

LogEvent now stores the client's pid (the pid defaults to -1 if it is
not known). This pid will be used internally for permission checks.

This CL also fixes a bug where uids of -1 were being treated incorrectly
as unsigned ints.

Test: bit statsd_test:* (passes when the new socket schema flag is
flipped on).

Change-Id: I8b6b202bb719b79646282764f82466c8065031b3
parent 0563e3b1
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ static void BM_LogEventCreation(benchmark::State& state) {
    uint8_t msg[LOGGER_ENTRY_MAX_PAYLOAD];
    size_t size = createAndParseStatsEvent(msg);
    while (state.KeepRunning()) {
        benchmark::DoNotOptimize(LogEvent(msg, size, /*uid=*/ 1000));
        benchmark::DoNotOptimize(LogEvent(msg, size, /*uid=*/ 1000, /*pid=*/ 1001));
    }
}
BENCHMARK(BM_LogEventCreation);
+1 −1
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ bool StatsCallbackPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
                    for (const StatsEventParcel& parcel: output) {
                        shared_ptr<LogEvent> event = make_shared<LogEvent>(
                                const_cast<uint8_t*>(parcel.buffer.data()), parcel.buffer.size(),
                                /*uid=*/-1, /*useNewSchema=*/true);
                                /*uid=*/-1, /*pid=*/-1, /*useNewSchema=*/true);
                        sharedData->push_back(event);
                    }
                    *pullSuccess = success;
+12 −4
Original line number Diff line number Diff line
@@ -37,11 +37,12 @@ using std::vector;

// Msg is expected to begin at the start of the serialized atom -- it should not
// include the android_log_header_t or the StatsEventTag.
LogEvent::LogEvent(uint8_t* msg, uint32_t len, uint32_t uid)
LogEvent::LogEvent(uint8_t* msg, uint32_t len, int32_t uid, int32_t pid)
    : mBuf(msg),
      mRemainingLen(len),
      mLogdTimestampNs(time(nullptr)),
      mLogUid(uid)
      mLogUid(uid),
      mLogPid(pid)
{
#ifdef NEW_ENCODING_SCHEME
    initNew();
@@ -52,8 +53,13 @@ LogEvent::LogEvent(uint8_t* msg, uint32_t len, uint32_t uid)
#endif
}

LogEvent::LogEvent(uint8_t* msg, uint32_t len, uint32_t uid, bool useNewSchema)
    : mBuf(msg), mRemainingLen(len), mLogdTimestampNs(time(nullptr)), mLogUid(uid) {
LogEvent::LogEvent(uint8_t* msg, uint32_t len, int32_t uid, int32_t pid, bool useNewSchema)
    : mBuf(msg),
      mRemainingLen(len),
      mLogdTimestampNs(time(nullptr)),
      mLogUid(uid),
      mLogPid(pid)
{
    if (useNewSchema) {
        initNew();
    } else {
@@ -66,6 +72,7 @@ LogEvent::LogEvent(uint8_t* msg, uint32_t len, uint32_t uid, bool useNewSchema)
LogEvent::LogEvent(const LogEvent& event) {
    mTagId = event.mTagId;
    mLogUid = event.mLogUid;
    mLogPid = event.mLogPid;
    mElapsedTimestampNs = event.mElapsedTimestampNs;
    mLogdTimestampNs = event.mLogdTimestampNs;
    mValues = event.mValues;
@@ -146,6 +153,7 @@ LogEvent::LogEvent(const string& trainName, int64_t trainVersionCode, bool requi
    mElapsedTimestampNs = getElapsedRealtimeNs();
    mTagId = android::util::BINARY_PUSH_STATE_CHANGED;
    mLogUid = android::IPCThreadState::self()->getCallingUid();
    mLogPid = android::IPCThreadState::self()->getCallingPid();

    mValues.push_back(FieldValue(Field(mTagId, getSimpleField(1)), Value(trainName)));
    mValues.push_back(FieldValue(Field(mTagId, getSimpleField(2)), Value(trainVersionCode)));
+19 −6
Original line number Diff line number Diff line
@@ -71,12 +71,12 @@ public:
    /**
     * Read a LogEvent from the socket
     */
    explicit LogEvent(uint8_t* msg, uint32_t len, uint32_t uid);
    explicit LogEvent(uint8_t* msg, uint32_t len, int32_t uid, int32_t pid);

    /**
     * Temp constructor to use for pulled atoms until we flip the socket schema.
     */
    explicit LogEvent(uint8_t* msg, uint32_t len, uint32_t uid, bool useNewSchema);
    explicit LogEvent(uint8_t* msg, uint32_t len, int32_t uid, int32_t pid, bool useNewSchema);

    /**
     * Constructs a LogEvent with synthetic data for testing. Must call init() before reading.
@@ -123,9 +123,17 @@ public:
     */
    inline int GetTagId() const { return mTagId; }

    inline uint32_t GetUid() const {
        return mLogUid;
    }
    /**
     * Get the uid of the logging client.
     * Returns -1 if the uid is unknown/has not been set.
     */
    inline int32_t GetUid() const { return mLogUid; }

    /**
     * Get the pid of the logging client.
     * Returns -1 if the pid is unknown/has not been set.
     */
    inline int32_t GetPid() const { return mLogPid; }

    /**
     * Get the nth value, starting at 1.
@@ -297,9 +305,14 @@ private:
    // The elapsed timestamp set by statsd log writer.
    int64_t mElapsedTimestampNs;

    // The atom tag of the event.
    int mTagId;

    uint32_t mLogUid;
    // The uid of the logging client (defaults to -1).
    int32_t mLogUid = -1;

    // The pid of the logging client (defaults to -1).
    int32_t mLogPid = -1;
};

void writeExperimentIdsToProto(const std::vector<int64_t>& experimentIds, std::vector<uint8_t>* protoOut);
+2 −1
Original line number Diff line number Diff line
@@ -126,9 +126,10 @@ bool StatsSocketListener::onDataAvailable(SocketClient* cli) {
    uint8_t* msg = ptr + sizeof(uint32_t);
    uint32_t len = n - sizeof(uint32_t);
    uint32_t uid = cred->uid;
    uint32_t pid = cred->pid;

    int64_t oldestTimestamp;
    if (!mQueue->push(std::make_unique<LogEvent>(msg, len, uid), &oldestTimestamp)) {
    if (!mQueue->push(std::make_unique<LogEvent>(msg, len, uid, pid), &oldestTimestamp)) {
        StatsdStats::getInstance().noteEventQueueOverflow(oldestTimestamp);
    }

Loading