Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

protection against starting of a (second) yacy peer while another one is already running on the same port

orbiter3da21c42662b
diff --git a/source/de/anomic/search/Switchboard.java b/source/de/anomic/search/Switchboard.java
index d41f1a9af..48f63e0be 100644
--- a/source/de/anomic/search/Switchboard.java
+++ b/source/de/anomic/search/Switchboard.java
@@ -66,6 +66,7 @@ import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.regex.Pattern;
@@ -87,6 +88,7 @@ import net.yacy.cora.protocol.Domains;
import net.yacy.cora.protocol.HeaderFramework;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.cora.protocol.ResponseHeader;
+import net.yacy.cora.protocol.TimeoutRequest;
import net.yacy.cora.protocol.http.HTTPClient;
import net.yacy.cora.protocol.http.ProxySettings;
import net.yacy.cora.services.federated.solr.SolrChardingConnector;
@@ -255,11 +257,21 @@ public final class Switchboard extends serverSwitch {
public Switchboard(final File dataPath, final File appPath, final String initPath, final String configPath) throws IOException {
super(dataPath, appPath, initPath, configPath);
+
+ // check if port is already occupied
+ final int port = getConfigInt("port", 8090);
+ try {
+ if (TimeoutRequest.ping("127.0.0.1", port, 500)) {
+ throw new RuntimeException("a server is already running on the YaCy port " + port + "; possibly another YaCy process has not terminated yet. Please stop YaCy before running a new instance.");
+ }
+ } catch (final ExecutionException e1) {
+ }
+
MemoryTracker.startSystemProfiling();
sb = this;
// set loglevel and log
- setLog(new Log("PLASMA"));
+ setLog(new Log("YACY_SEARCH"));
// set default peer name
yacySeed.ANON_PREFIX = getConfig("peernameprefix", "_anon");
diff --git a/source/net/yacy/yacy.java b/source/net/yacy/yacy.java
index 2fe10b096..51fa0d651 100644
--- a/source/net/yacy/yacy.java
+++ b/source/net/yacy/yacy.java
@@ -207,7 +207,12 @@ public final class yacy {
if(!oldconffile.renameTo(newconfFile))
Log.logSevere("STARTUP", "WARNING: the file " + oldconffile + " can not be renamed to "+ newconfFile +"!");
}
- sb = new Switchboard(dataHome, appHome, "defaults/yacy.init".replace("/", File.separator), newconf);
+ try {
+ sb = new Switchboard(dataHome, appHome, "defaults/yacy.init".replace("/", File.separator), newconf);
+ } catch (final RuntimeException e) {
+ Log.logSevere("STARTUP", "YaCy cannot start: " + e.getMessage());
+ System.exit(-1);
+ }
//sbSync.V(); // signal that the sb reference was set
// switch the memory strategy