Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Commit

Permalink
[Fpm] Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
miniBill committed Sep 5, 2013
1 parent 8edfaf4 commit 29f5581
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 44 deletions.
87 changes: 48 additions & 39 deletions src/Mono.WebServer.Fpm/ChildrenManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,55 +81,25 @@ public static void StartChildren(FileInfo[] configFiles, ConfigurationManager co
foreach (FileInfo fileInfo in configFiles) {
if (fileInfo == null)
continue;
var childConfigurationManager = new ChildConfigurationManager ("child-" + fileInfo.Name);
Logger.Write (LogLevel.Debug, "Loaded {0} [{1}]", fileInfo.Name, childConfigurationManager.InstanceType.ToString ().ToLowerInvariant ());
string configFile = fileInfo.FullName;
if (!childConfigurationManager.TryLoadXmlConfig (configFile))
var filename = fileInfo.Name;
var childConfigurationManager = new ChildConfigurationManager ("child-" + filename);
Logger.Write (LogLevel.Debug, "Loaded {0} [{1}]", filename, childConfigurationManager.InstanceType.ToString ().ToLowerInvariant ());
string fullFilename = fileInfo.FullName;
if (!childConfigurationManager.TryLoadXmlConfig (fullFilename))
continue;
string fastCgiCommand = configurationManager.FastCgiCommand;

Func<Process> spawner;
if (childConfigurationManager.InstanceType == InstanceType.Ondemand) {
if (String.IsNullOrEmpty (childConfigurationManager.ShimSocket))
throw new Exception ("You must specify a socket for the shim");
spawner = () => Spawner.SpawnOndemandChild (childConfigurationManager.ShimSocket);
} else
spawner = () => Spawner.SpawnStaticChild (configFile, fastCgiCommand);
var spawner = GetSpawner (configurationManager.ShimCommand, filename, childConfigurationManager, fullFilename, configurationManager.FastCgiCommand);

Action spawnShim = () => Spawner.SpawnShim (configurationManager.ShimCommand, childConfigurationManager.ShimSocket, configFile, fastCgiCommand);

string user = childConfigurationManager.User;
string group = childConfigurationManager.Group;
if (String.IsNullOrEmpty (user)) {
if (Platform.IsUnix) {
Logger.Write (LogLevel.Warning, "Configuration file {0} didn't specify username, defaulting to file owner", fileInfo.Name);
string owner = UnixFileSystemInfo.GetFileSystemEntry (configFile).OwnerUser.UserName;
if (childConfigurationManager.InstanceType == InstanceType.Ondemand)
Spawner.RunAs (owner, group, spawnShim) ();
else
spawner = Spawner.RunAs (owner, group, spawner);
} else {
Logger.Write (LogLevel.Warning, "Configuration file {0} didn't specify username, defaulting to the current one", fileInfo.Name);
if (childConfigurationManager.InstanceType != InstanceType.Ondemand)
spawnShim ();
}
} else {
if (childConfigurationManager.InstanceType == InstanceType.Ondemand)
Spawner.RunAs (user, group, spawnShim) ();
else
spawner = Spawner.RunAs (user, group, spawner);
}

var child = new ChildInfo { Spawner = spawner, ConfigurationManager = childConfigurationManager, Name = configFile };
var child = new ChildInfo { Spawner = spawner, ConfigurationManager = childConfigurationManager, Name = fullFilename };
children.Add (child);

if (childConfigurationManager.InstanceType == InstanceType.Static) {
if (child.TrySpawn ()) {
Logger.Write (LogLevel.Notice, "Started fastcgi daemon [static] with pid {0} and config file {1}", child.Process.Id, Path.GetFileName (configFile));
Logger.Write (LogLevel.Notice, "Started fastcgi daemon [static] with pid {0} and config file {1}", child.Process.Id, Path.GetFileName (fullFilename));
Thread.Sleep (500);
// TODO: improve this (it's used to wait for the child to be ready)
} else
Logger.Write (LogLevel.Error, "Couldn't start child with config file {0}", configFile);
Logger.Write (LogLevel.Error, "Couldn't start child with config file {0}", fullFilename);
break;
} else {
Socket socket;
Expand All @@ -140,5 +110,44 @@ public static void StartChildren(FileInfo[] configFiles, ConfigurationManager co
}
}
}

static Func<Process> GetSpawner (string shimCommand, string filename, ChildConfigurationManager childConfigurationManager, string configFile, string fastCgiCommand)
{
Func<Process> spawner;
if (childConfigurationManager.InstanceType == InstanceType.Ondemand) {
if (String.IsNullOrEmpty (childConfigurationManager.ShimSocket))
throw new Exception ("You must specify a socket for the shim");
spawner = () => Spawner.SpawnOndemandChild (childConfigurationManager.ShimSocket);
}
else
spawner = () => Spawner.SpawnStaticChild (configFile, fastCgiCommand);

Action spawnShim = () => Spawner.SpawnShim (shimCommand, childConfigurationManager.ShimSocket, configFile, fastCgiCommand);
string user = childConfigurationManager.User;
string group = childConfigurationManager.Group;
if (String.IsNullOrEmpty (user)) {
if (Platform.IsUnix) {
Logger.Write (LogLevel.Warning, "Configuration file {0} didn't specify username, defaulting to file owner", filename);
string owner = UnixFileSystemInfo.GetFileSystemEntry (configFile).OwnerUser.UserName;
if (childConfigurationManager.InstanceType == InstanceType.Ondemand)
Spawner.RunAs (owner, group, spawnShim) ();
else
spawner = Spawner.RunAs (owner, group, spawner);
}
else {
Logger.Write (LogLevel.Warning, "Configuration file {0} didn't specify username, defaulting to the current one", filename);
if (childConfigurationManager.InstanceType != InstanceType.Ondemand)
spawnShim ();
}
}
else {
if (childConfigurationManager.InstanceType == InstanceType.Ondemand)
Spawner.RunAs (user, group, spawnShim) ();
else
spawner = Spawner.RunAs (user, group, spawner);
}

return spawner;
}
}
}
10 changes: 7 additions & 3 deletions src/Mono.WebServer.Fpm/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ class ConfigurationManager : Options.ConfigurationManager, IHelpConfigurationMan
readonly StringSetting configDir = new StringSetting ("config-dir", "Directory containing the configuration files.");
readonly StringSetting fastCgiCommand = new StringSetting ("fastcgi-command", "Name (if in PATH) or full path of the fastcgi executable", defaultValue: "fastcgi-mono-server4");
readonly StringSetting shimCommand = new StringSetting ("shim-command", "Name (if in PATH) or full path of the shim executable", defaultValue: "shim");
readonly StringSetting fpmUser = new StringSetting("fpm-user", "Name of the user to use for the fpm daemon", defaultValue: "fpm");
readonly StringSetting fpmGroup = new StringSetting("fpm-group", "Name of the group to use for the fpm daemon", defaultValue: "fpm");
readonly StringSetting fpmUser = new StringSetting ("fpm-user", "Name of the user to use for the fpm daemon", defaultValue: "fpm");
readonly StringSetting fpmGroup = new StringSetting ("fpm-group", "Name of the group to use for the fpm daemon", defaultValue: "fpm");
readonly StringSetting webDirs = new StringSetting ("webdirs", "Directory containing the user web directories.");
#endregion

#region Typesafe properties
Expand All @@ -61,6 +62,9 @@ public string FpmUser {
public string FpmGroup {
get { return fpmGroup; }
}
public string WebDirs {
get { return webDirs; }
}
#endregion

public string ProgramName {
Expand All @@ -73,7 +77,7 @@ public string Description {

public ConfigurationManager (string name) : base(name)
{
Add (stoppable, configDir, fastCgiCommand, fpmUser, fpmGroup);
Add (stoppable, configDir, fastCgiCommand, fpmUser, fpmGroup, webDirs);
}
}
}
4 changes: 2 additions & 2 deletions src/Mono.WebServer/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static bool IsUnix {

static uint GetUid (string user)
{
UnixUserInfo info = new UnixUserInfo (user);
var info = new UnixUserInfo (user);
long uid = info.UserId;
if (uid > UInt32.MaxValue || uid <= 0)
throw new ArgumentOutOfRangeException ("user", String.Format ("Uid for {0} ({1}) not in range for suid", user, uid));
Expand All @@ -52,7 +52,7 @@ static uint GetUid (string user)

static uint GetGid (string group)
{
UnixGroupInfo info = new UnixGroupInfo (group);
var info = new UnixGroupInfo (group);
var gid = info.GroupId;
if (gid > UInt32.MaxValue || gid <= 0)
throw new ArgumentOutOfRangeException ("group", String.Format ("Gid for {0} ({1}) not in range for sgid", group, gid));
Expand Down

0 comments on commit 29f5581

Please sign in to comment.