Switch to unified view

a/upmpd/upmpd.cxx b/upmpd/upmpd.cxx
...
...
16
 */
16
 */
17
17
18
#include <stdio.h>
18
#include <stdio.h>
19
#include <stdlib.h>
19
#include <stdlib.h>
20
#include <unistd.h>
20
#include <unistd.h>
21
#include <sys/types.h>
22
#include <pwd.h>
21
23
22
#include <string>
24
#include <string>
23
#include <iostream>
25
#include <iostream>
24
#include <sstream>
26
#include <sstream>
25
#include <vector>
27
#include <vector>
...
...
1188
1190
1189
int main(int argc, char *argv[])
1191
int main(int argc, char *argv[])
1190
{
1192
{
1191
    string mpdhost("localhost");
1193
    string mpdhost("localhost");
1192
    int mpdport = 6600;
1194
    int mpdport = 6600;
1193
//    string upnplogfilename("/tmp/upmpd_libupnp.log");
1195
  // string upnplogfilename("/tmp/upmpd_libupnp.log");
1194
    string logfilename;
1196
    string logfilename;
1195
    int loglevel(upnppdebug::Logger::LLINF);
1197
    int loglevel(upnppdebug::Logger::LLINF);
1196
    string configfile;
1198
    string configfile;
1197
    string friendlyname(dfltFriendlyName);
1199
    string friendlyname(dfltFriendlyName);
1198
    bool ownqueue = true;
1200
    bool ownqueue = true;
1201
  string upmpdcliuser("upmpdcli");
1202
  string pidfilename("/var/run/upmpdcli.run");
1199
1203
1200
    const char *cp;
1204
    const char *cp;
1201
    if ((cp = getenv("UPMPD_HOST")))
1205
    if ((cp = getenv("UPMPD_HOST")))
1202
        mpdhost = cp;
1206
        mpdhost = cp;
1203
    if ((cp = getenv("UPMPD_PORT")))
1207
    if ((cp = getenv("UPMPD_PORT")))
...
...
1265
        cerr << "Can't initialize log" << endl;
1269
        cerr << "Can't initialize log" << endl;
1266
        return 1;
1270
        return 1;
1267
    }
1271
    }
1268
    upnppdebug::Logger::getTheLog("")->setLogLevel(upnppdebug::Logger::LogLevel(loglevel));
1272
    upnppdebug::Logger::getTheLog("")->setLogLevel(upnppdebug::Logger::LogLevel(loglevel));
1269
1273
1274
    Pidfile pidfile(pidfilename);
1275
1276
  // If started by root, do the pidfile + change uid thing
1277
  uid_t runas(0);
1278
  if (geteuid() == 0) {
1279
      struct passwd *pass = getpwnam(upmpdcliuser.c_str());
1280
      if (pass == 0) {
1281
          LOGFAT("upmpdcli won't run as root and user " << upmpdcliuser << 
1282
                 " does not exist " << endl);
1283
          return 1;
1284
      }
1285
      runas = pass->pw_uid;
1286
1287
      pid_t pid;
1288
      if ((pid = pidfile.open()) != 0) {
1289
          LOGFAT("Can't open pidfile: " << pidfile.getreason() << 
1290
                 ". Return (other pid?): " << pid << endl);
1291
          return 1;
1292
      }
1293
      if (pidfile.write_pid() != 0) {
1294
          LOGFAT("Can't write pidfile: " << pidfile.getreason() << endl);
1295
          return 1;
1296
      }
1297
  }
1298
1270
    if ((op_flags & OPT_D)) {
1299
    if ((op_flags & OPT_D)) {
1271
        if (daemon(1, 0)) {
1300
        if (daemon(1, 0)) {
1272
            LOGFAT("Daemon failed: errno " << errno << endl);
1301
            LOGFAT("Daemon failed: errno " << errno << endl);
1273
            return 1;
1302
            return 1;
1274
        }
1303
        }
1304
  }
1305
1306
  if (geteuid() == 0) {
1307
      // Need to rewrite pid, it may have changed with the daemon call
1308
      pidfile.write_pid();
1309
      setuid(runas);
1275
    }
1310
    }
1276
1311
1277
    // Initialize libupnpp, and check health
1312
    // Initialize libupnpp, and check health
1278
    LibUPnP *mylib = LibUPnP::getLibUPnP(true);
1313
    LibUPnP *mylib = LibUPnP::getLibUPnP(true);
1279
    if (!mylib) {
1314
    if (!mylib) {