@@ -75,50 +75,39 @@ public void tag(FileDescriptor fd) throws SocketException {
7575 Log .d (TAG , "tagSocket(" + fd .getInt$ () + ") with statsTag=0x"
7676 + Integer .toHexString (options .statsTag ) + ", statsUid=" + options .statsUid );
7777 }
78- try {
79- // TODO: skip tagging when options would be no-op
80- tagSocketFd (fd , options .statsTag , options .statsUid );
81- } catch (IOException e ) {
82- throw new SocketException ("Problem tagging socket" , e );
83- }
78+ // TODO: skip tagging when options would be no-op
79+ tagSocketFd (fd , options .statsTag , options .statsUid );
8480 }
8581
86- private void tagSocketFd (FileDescriptor fd , int tag , int uid ) throws IOException {
87- final int fdNum = fd .getInt$ ();
88- if (fdNum == -1 || (tag == -1 && uid == -1 )) return ;
89-
90- String cmd = "t " + fdNum ;
91- if (tag == -1 ) {
92- // Case where just the uid needs adjusting. But probably the caller
93- // will want to track his own name here, just in case.
94- cmd += " 0" ;
95- } else {
96- cmd += " " + tagToKernel (tag );
97- }
98- if (uid != -1 ) {
99- cmd += " " + uid ;
82+ private void tagSocketFd (FileDescriptor fd , int tag , int uid ) {
83+ int errno ;
84+ if (tag == -1 && uid == -1 ) return ;
85+
86+ errno = native_tagSocketFd (fd , tag , uid );
87+ if (errno < 0 ) {
88+ Log .i (TAG , "tagSocketFd(" + fd .getInt$ () + ", "
89+ + tag + ", " +
90+ + uid + ") failed with errno" + errno );
10091 }
101- internalModuleCtrl (cmd );
10292 }
10393
10494 @ Override
10595 public void untag (FileDescriptor fd ) throws SocketException {
10696 if (LOGD ) {
10797 Log .i (TAG , "untagSocket(" + fd .getInt$ () + ")" );
10898 }
109- try {
110- unTagSocketFd (fd );
111- } catch (IOException e ) {
112- throw new SocketException ("Problem untagging socket" , e );
113- }
99+ unTagSocketFd (fd );
114100 }
115101
116- private void unTagSocketFd (FileDescriptor fd ) throws IOException {
117- int fdNum = fd .getInt$ ();
102+ private void unTagSocketFd (FileDescriptor fd ) {
118103 final SocketTags options = threadSocketTags .get ();
119- if (fdNum == -1 || (options .statsTag == -1 && options .statsUid == -1 )) return ;
120- String cmd = "u " + fdNum ;
121- internalModuleCtrl (cmd );
104+ int errno ;
105+ if (options .statsTag == -1 && options .statsUid == -1 ) return ;
106+
107+ errno = native_untagSocketFd (fd );
108+ if (errno < 0 ) {
109+ Log .w (TAG , "untagSocket(" + fd .getInt$ () + ") failed with errno " + errno );
110+ }
122111 }
123112
124113 public static class SocketTags {
@@ -127,68 +116,19 @@ public static class SocketTags {
127116 }
128117
129118 public static void setKernelCounterSet (int uid , int counterSet ) {
130- final StringBuilder command = new StringBuilder ();
131- command .append ("s " ).append (counterSet ).append (" " ).append (uid );
132- try {
133- internalModuleCtrl (command .toString ());
134- } catch (IOException e ) {
135- Slog .w (TAG , "problem changing counter set for uid " + uid + " : " + e );
119+ int errno = native_setCounterSet (counterSet , uid );
120+ if (errno < 0 ) {
121+ Log .w (TAG , "setKernelCountSet(" + uid + ", " + counterSet + ") failed with errno " + errno );
136122 }
137123 }
138124
139125 public static void resetKernelUidStats (int uid ) {
140- final StringBuilder command = new StringBuilder ();
141- command .append ("d 0 " ).append (uid );
142- try {
143- internalModuleCtrl (command .toString ());
144- } catch (IOException e ) {
145- Slog .w (TAG , "problem clearing counters for uid " + uid + " : " + e );
126+ int errno = native_deleteTagData (0 , uid );
127+ if (errno < 0 ) {
128+ Slog .w (TAG , "problem clearing counters for uid " + uid + " : errno " + errno );
146129 }
147130 }
148131
149- /**
150- * Sends commands to the kernel netfilter module.
151- *
152- * @param cmd command string for the qtaguid netfilter module. May not be null.
153- * <p>Supports:
154- * <ul><li>tag a socket:<br>
155- * <code>t <i>sock_fd</i> <i>acct_tag</i> [<i>uid_in_case_caller_is_acting_on_behalf_of</i>]</code><br>
156- * <code>*_tag</code> defaults to default_policy_tag_from_uid(uid_of_caller)<br>
157- * <code>acct_tag</code> is either 0 or greater that 2^32.<br>
158- * <code>uid_*</code> is only settable by privileged UIDs (DownloadManager,...)
159- * </li>
160- * <li>untag a socket, preserving counters:<br>
161- * <code>u <i>sock_fd</i></code>
162- * </li></ul>
163- * <p>Notes:<br>
164- * <ul><li><i>sock_fd</i> is withing the callers process space.</li>
165- * <li><i>*_tag</i> are 64bit values</li></ul>
166- *
167- */
168- private static void internalModuleCtrl (String cmd ) throws IOException {
169- if (!SystemProperties .getBoolean (PROP_QTAGUID_ENABLED , false )) return ;
170-
171- // TODO: migrate to native library for tagging commands
172- FileOutputStream procOut = null ;
173- try {
174- procOut = new FileOutputStream ("/proc/net/xt_qtaguid/ctrl" );
175- procOut .write (cmd .getBytes (Charsets .US_ASCII ));
176- } finally {
177- IoUtils .closeQuietly (procOut );
178- }
179- }
180-
181- /**
182- * Convert {@link Integer} tag to {@code /proc/} format. Assumes unsigned
183- * base-10 format like {@code 2147483647}. Currently strips signed bit to
184- * avoid using {@link BigInteger}.
185- */
186- public static String tagToKernel (int tag ) {
187- // TODO: eventually write in hex, since that's what proc exports
188- // TODO: migrate to direct integer instead of odd shifting
189- return Long .toString ((((long ) tag ) << 32 ) & 0x7FFFFFFF00000000L );
190- }
191-
192132 /**
193133 * Convert {@code /proc/} tag format to {@link Integer}. Assumes incoming
194134 * format like {@code 0x7fffffff00000000}.
@@ -197,4 +137,9 @@ public static int kernelToTag(String string) {
197137 // TODO: migrate to direct integer instead of odd shifting
198138 return (int ) (Long .decode (string ) >> 32 );
199139 }
140+
141+ private static native int native_tagSocketFd (FileDescriptor fd , int tag , int uid );
142+ private static native int native_untagSocketFd (FileDescriptor fd );
143+ private static native int native_setCounterSet (int uid , int counterSetNum );
144+ private static native int native_deleteTagData (int tag , int uid );
200145}
0 commit comments