Ashish Sharma | 8626cce | 2011-07-07 18:16:01 -0700 | [diff] [blame] | 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> |
Ashish Sharma | 9b5c774 | 2011-08-03 00:31:19 -0700 | [diff] [blame^] | 22 | #include <errno.h> |
Ashish Sharma | 8626cce | 2011-07-07 18:16:01 -0700 | [diff] [blame] | 23 | #include <fcntl.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <unistd.h> |
| 27 | |
Ashish Sharma | 9b5c774 | 2011-08-03 00:31:19 -0700 | [diff] [blame^] | 28 | extern int qtaguid_tagSocket(int sockfd, int tag, uid_t uid) { |
Ashish Sharma | 8626cce | 2011-07-07 18:16:01 -0700 | [diff] [blame] | 29 | char lineBuf[128]; |
Ashish Sharma | 9b5c774 | 2011-08-03 00:31:19 -0700 | [diff] [blame^] | 30 | int fd, cnt = 0, res = 0; |
Ashish Sharma | 8626cce | 2011-07-07 18:16:01 -0700 | [diff] [blame] | 31 | uint64_t kTag = (uint64_t)tag << 32; |
| 32 | snprintf(lineBuf, sizeof(lineBuf), "t %d %llu %d", sockfd, kTag, uid); |
| 33 | |
Ashish Sharma | 9b5c774 | 2011-08-03 00:31:19 -0700 | [diff] [blame^] | 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 | } |
Ashish Sharma | 8626cce | 2011-07-07 18:16:01 -0700 | [diff] [blame] | 39 | |
Ashish Sharma | 9b5c774 | 2011-08-03 00:31:19 -0700 | [diff] [blame^] | 40 | cnt = write(fd, lineBuf, strlen(lineBuf)); |
| 41 | if (cnt < 0) { |
| 42 | res = -errno; |
| 43 | } |
| 44 | |
| 45 | close(fd); |
| 46 | return res; |
| 47 | } |
| 48 | |
| 49 | extern 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 Sharma | 8626cce | 2011-07-07 18:16:01 -0700 | [diff] [blame] | 67 | } |