blob: 218a21f13f3a7fcdfabaac1a12d5c15e6015c3bd [file] [log] [blame]
Ashish Sharma8626cce2011-07-07 18:16:01 -07001/* 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>
Ashish Sharma9b5c7742011-08-03 00:31:19 -070022#include <errno.h>
Ashish Sharma8626cce2011-07-07 18:16:01 -070023#include <fcntl.h>
24#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
27
Ashish Sharma9b5c7742011-08-03 00:31:19 -070028extern int qtaguid_tagSocket(int sockfd, int tag, uid_t uid) {
Ashish Sharma8626cce2011-07-07 18:16:01 -070029 char lineBuf[128];
Ashish Sharma9b5c7742011-08-03 00:31:19 -070030 int fd, cnt = 0, res = 0;
Ashish Sharma8626cce2011-07-07 18:16:01 -070031 uint64_t kTag = (uint64_t)tag << 32;
32 snprintf(lineBuf, sizeof(lineBuf), "t %d %llu %d", sockfd, kTag, uid);
33
Ashish Sharma9b5c7742011-08-03 00:31:19 -070034 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 }
Ashish Sharma8626cce2011-07-07 18:16:01 -070039
Ashish Sharma9b5c7742011-08-03 00:31:19 -070040 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;
Ashish Sharma8626cce2011-07-07 18:16:01 -070067}