Fix: lttng-ust-jul: set thread in daemon mode
[lttng-ust.git] / liblttng-ust-jul / org / lttng / ust / jul / LTTngAgent.java
CommitLineData
43e5396b
DG
1/*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
7 *
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11 * for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18package org.lttng.ust.jul;
19
20import java.io.IOException;
43e5396b
DG
21import java.io.InputStream;
22import java.io.BufferedReader;
23import java.io.FileReader;
24import java.util.concurrent.Semaphore;
f1fa0535 25import java.util.concurrent.TimeUnit;
43e5396b
DG
26import java.util.logging.FileHandler;
27import java.util.logging.Handler;
28import java.util.logging.Level;
29import java.util.logging.Logger;
30import java.util.logging.LogManager;
31import java.util.Enumeration;
32
33public class LTTngAgent {
43e5396b 34 private static LogManager logManager;
9aabed2d
DG
35
36 /* Possible that we have to threads handling two sessiond. */
37 private static LTTngLogHandler lttngHandlerRoot;
38 private static LTTngLogHandler lttngHandlerUser;
39 private static LTTngThread lttngThreadRoot;
40 private static LTTngThread lttngThreadUser;
41 private static Thread sessiondThRoot;
42 private static Thread sessiondThUser;
43e5396b
DG
43
44 /* Singleton agent object. */
45 private static LTTngAgent curAgent = null;
46
47 /* Indicate if this object has been initialized. */
48 private static boolean initialized = false;
49
50 private static Semaphore registerSem;
f1fa0535 51 private final static int semTimeout = 3; /* Seconds */
43e5396b 52
f1fa0535
DG
53 /*
54 * Default value to connect to session daemon. Port number is dynamically
55 * fetched from the port file that is created by a running session daemon.
56 */
43e5396b 57 private static final String sessiondAddr = "127.0.0.1";
43e5396b
DG
58
59 /*
60 * Constructor is private. This is a singleton and a reference should be
61 * acquired using getLTTngAgent().
62 */
63 private LTTngAgent() throws IOException {
64 this.logManager = LogManager.getLogManager();
9aabed2d
DG
65 this.lttngHandlerUser = new LTTngLogHandler(this.logManager);
66 this.lttngHandlerRoot = new LTTngLogHandler(this.logManager);
67 this.lttngHandlerRoot.is_root = 1;
43e5396b
DG
68 this.registerSem = new Semaphore(0, true);
69 }
70
71 private void removeHandlers() throws SecurityException, IOException {
72 String loggerName;
73 Logger logger;
74
75 Enumeration list = this.logManager.getLoggerNames();
76 while (list.hasMoreElements()) {
77 loggerName = list.nextElement().toString();
78 /* Somehow there is always an empty string at the end. */
79 if (loggerName == "") {
80 continue;
81 }
82
83 logger = this.logManager.getLogger(loggerName);
9aabed2d
DG
84 logger.removeHandler(this.lttngHandlerUser);
85 logger.removeHandler(this.lttngHandlerRoot);
43e5396b
DG
86 }
87 }
88
89 private int getUID() throws IOException {
90 int uid;
91 byte b[] = new byte[4];
92 String userName = System.getProperty("user.name");
93 String command = "id -u " + userName;
94 Process child = Runtime.getRuntime().exec(command);
95 InputStream in = child.getInputStream();
96
97 in.read(b);
98 uid = Integer.parseInt(new String(b).trim(), 10);
99 in.close();
100
101 return uid;
102 }
103
43e5396b
DG
104 /*
105 * Public getter to acquire a reference to this singleton object.
106 */
107 public static synchronized LTTngAgent getLTTngAgent() throws IOException {
108 if (curAgent == null) {
109 curAgent = new LTTngAgent();
110 curAgent.init();
111 }
112
113 return curAgent;
114 }
115
116 /*
117 * Initialize LTTngAgent. This will attach the log handler to all Logger
118 * returned by the logManager.
119 */
120 private synchronized void init() throws SecurityException, IOException {
9aabed2d
DG
121 int nr_acquires = 0;
122
43e5396b
DG
123 if (this.initialized) {
124 return;
125 }
126
9aabed2d 127 /* Handle user session daemon if any. */
f1fa0535 128 this.lttngThreadUser = new LTTngThread(this.sessiondAddr,
9aabed2d
DG
129 this.lttngHandlerUser, this.registerSem);
130 this.sessiondThUser = new Thread(lttngThreadUser);
0c52a51d 131 this.sessiondThUser.setDaemon(true);
9aabed2d
DG
132 this.sessiondThUser.start();
133 /* Wait for registration done of per-user sessiond */
134 nr_acquires++;
135
f1fa0535
DG
136 /* Handle root session daemon. */
137 this.lttngThreadRoot = new LTTngThread(this.sessiondAddr,
138 this.lttngHandlerRoot, this.registerSem);
139 this.sessiondThRoot = new Thread(lttngThreadRoot);
0c52a51d 140 this.sessiondThRoot.setDaemon(true);
f1fa0535
DG
141 this.sessiondThRoot.start();
142 /* Wait for registration done of system-wide sessiond */
143 nr_acquires++;
43e5396b 144
9aabed2d 145 /* Wait for each registration to end. */
43e5396b 146 try {
f1fa0535
DG
147 this.registerSem.tryAcquire(nr_acquires, semTimeout,
148 TimeUnit.SECONDS);
43e5396b
DG
149 } catch (InterruptedException e) {
150 e.printStackTrace();
151 }
9aabed2d
DG
152
153 this.initialized = true;
43e5396b
DG
154 }
155
156 public void dispose() throws IOException {
9aabed2d
DG
157 this.lttngThreadUser.dispose();
158 if (this.lttngThreadRoot != null) {
159 this.lttngThreadRoot.dispose();
160 }
43e5396b
DG
161
162 /* Make sure there is no more LTTng handler attach to logger(s). */
163 this.removeHandlers();
164
165 try {
9aabed2d
DG
166 this.sessiondThUser.join();
167 if (this.sessiondThRoot != null) {
168 this.sessiondThRoot.join();
169 }
43e5396b
DG
170 } catch (InterruptedException e) {
171 e.printStackTrace();
172 }
173 }
174}
This page took 0.030533 seconds and 4 git commands to generate.