qtaguid.c revision 9b5c774369fcbd4460751f9b01e692d688c386ba
1/* libcutils/qtaguid.c
2**
3** Copyright 2011, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "qtaguid"
19
20#include <cutils/qtaguid.h>
21#include <cutils/log.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
27
28extern int qtaguid_tagSocket(int sockfd, int tag, uid_t uid) {
29    char lineBuf[128];
30    int fd, cnt = 0, res = 0;
31    uint64_t kTag = (uint64_t)tag << 32;
32    snprintf(lineBuf, sizeof(lineBuf), "t %d %llu %d", sockfd, kTag, uid);
33
34    LOGI("Tagging socket %d with tag %llx(%d) for uid %d", sockfd, kTag, tag, uid);
35    fd = open("/proc/net/xt_qtaguid/ctrl", O_WRONLY);
36    if (fd < 0) {
37        return -errno;
38    }
39
40    cnt = write(fd, lineBuf, strlen(lineBuf));
41    if (cnt < 0) {
42        res = -errno;
43    }
44
45    close(fd);
46    return res;
47}
48
49extern int qtaguid_untagSocket(int sockfd) {
50    char lineBuf[128];
51    int fd, cnt = 0, res = 0;
52    snprintf(lineBuf, sizeof(lineBuf), "u %d", sockfd);
53
54    LOGI("Untagging socket %d", sockfd);
55    fd = open("/proc/net/xt_qtaguid/ctrl", O_WRONLY);
56    if (fd < 0) {
57        return -errno;
58    }
59
60    cnt = write(fd, lineBuf, strlen(lineBuf));
61    if (cnt < 0) {
62        res = -errno;
63    }
64
65    close(fd);
66    return res;
67}
68