main() cleanup
git-svn-id: https://svn.musicpd.org/mpd/trunk@1298 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
parent
1200187857
commit
53ac376cdc
2
TODO
2
TODO
@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
2) ACK error codes
|
2) ACK error codes
|
||||||
|
|
||||||
3) cleanup main()
|
|
||||||
|
|
||||||
|
|
||||||
Post-1.0
|
Post-1.0
|
||||||
--------
|
--------
|
||||||
|
168
src/main.c
168
src/main.c
@ -201,11 +201,7 @@ void parseOptions(int argc, char ** argv, Options * options) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char * argv[]) {
|
void closeAllFDs() {
|
||||||
int port, uid, gid;
|
|
||||||
FILE * out;
|
|
||||||
FILE * err;
|
|
||||||
Options options;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for(i=0;i<FD_SETSIZE;i++) {
|
for(i=0;i<FD_SETSIZE;i++) {
|
||||||
@ -218,113 +214,106 @@ int main(int argc, char * argv[]) {
|
|||||||
close(i);
|
close(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
initConf();
|
void establishListen(Options * options) {
|
||||||
|
int port;
|
||||||
|
|
||||||
parseOptions(argc,argv,&options);
|
if((port = atoi(options->portStr))<0) {
|
||||||
|
|
||||||
initStats();
|
|
||||||
initLog();
|
|
||||||
|
|
||||||
if((port = atoi(options.portStr))<0) {
|
|
||||||
ERROR("problem with port number\n");
|
ERROR("problem with port number\n");
|
||||||
return EXIT_FAILURE;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!options.createDB && !options.updateDB &&
|
if(!options->createDB && !options->updateDB &&
|
||||||
(listenSocket = establish(port))<0)
|
(listenSocket = establish(port))<0)
|
||||||
{
|
{
|
||||||
ERROR("error binding port\n");
|
ERROR("error binding port\n");
|
||||||
return EXIT_FAILURE;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
void changeToUser(Options * options) {
|
||||||
* lose privileges as early as possible
|
if (options->usr && strlen(options->usr)) {
|
||||||
*/
|
int uid, gid;
|
||||||
|
|
||||||
/* change uid */
|
|
||||||
if (options.usr && strlen(options.usr)) {
|
|
||||||
#ifdef _BSD_SOURCE
|
#ifdef _BSD_SOURCE
|
||||||
gid_t gid_list[NGROUPS_MAX];
|
gid_t gid_list[NGROUPS_MAX];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* get uid */
|
/* get uid */
|
||||||
struct passwd * userpwd;
|
struct passwd * userpwd;
|
||||||
if ((userpwd = getpwnam(options.usr)) == NULL) {
|
if ((userpwd = getpwnam(options->usr)) == NULL) {
|
||||||
ERROR("no such user: %s\n", options.usr);
|
ERROR("no such user: %s\n", options->usr);
|
||||||
return EXIT_FAILURE;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
uid = userpwd->pw_uid;
|
uid = userpwd->pw_uid;
|
||||||
gid = userpwd->pw_gid;
|
gid = userpwd->pw_gid;
|
||||||
|
|
||||||
if(setgid(gid) == -1) {
|
if(setgid(gid) == -1) {
|
||||||
ERROR("cannot setgid of user %s: %s\n", options.usr,
|
ERROR("cannot setgid of user %s: %s\n", options->usr,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return EXIT_FAILURE;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _BSD_SOURCE
|
#ifdef _BSD_SOURCE
|
||||||
/* init suplementary groups
|
/* init suplementary groups
|
||||||
* (must be done before we change our uid)
|
* (must be done before we change our uid)
|
||||||
*/
|
*/
|
||||||
if (initgroups(options.usr, gid) == -1) {
|
if (initgroups(options->usr, gid) == -1) {
|
||||||
ERROR("cannot init suplementary groups "
|
ERROR("cannot init suplementary groups "
|
||||||
"of user %s: %s\n", options.usr,
|
"of user %s: %s\n", options->usr,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
else if(getgroups(NGROUPS_MAX, gid_list) == -1) {
|
else if(getgroups(NGROUPS_MAX, gid_list) == -1) {
|
||||||
ERROR("cannot get groups "
|
ERROR("cannot get groups "
|
||||||
"of user %s: %s\n", options.usr,
|
"of user %s: %s\n", options->usr,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return EXIT_FAILURE;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
else if(setgroups(NGROUPS_MAX, gid_list) == -1) {
|
else if(setgroups(NGROUPS_MAX, gid_list) == -1) {
|
||||||
ERROR("cannot set groups "
|
ERROR("cannot set groups "
|
||||||
"of user %s: %s\n", options.usr,
|
"of user %s: %s\n", options->usr,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return EXIT_FAILURE;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* set uid */
|
/* set uid */
|
||||||
if (setuid(uid) == -1) {
|
if (setuid(uid) == -1) {
|
||||||
ERROR("cannot change to uid of user "
|
ERROR("cannot change to uid of user "
|
||||||
"%s: %s\n", options.usr,
|
"%s: %s\n", options->usr,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return EXIT_FAILURE;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(NULL==(out=fopen(options.logFile,"a"))) {
|
void openLogFiles(Options * options, FILE ** out, FILE ** err) {
|
||||||
|
if(options->stdOutput) return;
|
||||||
|
|
||||||
|
if(NULL==(*out=fopen(options->logFile,"a"))) {
|
||||||
ERROR("problem opening file \"%s\" for writing\n",
|
ERROR("problem opening file \"%s\" for writing\n",
|
||||||
options.logFile);
|
options->logFile);
|
||||||
return EXIT_FAILURE;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(NULL==(err=fopen(options.errorFile,"a"))) {
|
if(NULL==(*err=fopen(options->errorFile,"a"))) {
|
||||||
ERROR("problem opening file \"%s\" for writing\n",
|
ERROR("problem opening file \"%s\" for writing\n",
|
||||||
options.errorFile);
|
options->errorFile);
|
||||||
return EXIT_FAILURE;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
initPaths(options.playlistDirArg,options.musicDirArg);
|
void openDB(Options * options, char * argv0) {
|
||||||
initPermissions();
|
if(!options->dbFile) directory_db = strdup(rpp2app(".mpddb"));
|
||||||
initReplayGainState();
|
else directory_db = strdup(options->dbFile);
|
||||||
|
|
||||||
initTables();
|
if(options->createDB>0 || readDirectoryDB()<0) {
|
||||||
initPlaylist();
|
if(options->createDB<0) {
|
||||||
initInputPlugins();
|
|
||||||
|
|
||||||
if(!options.dbFile) directory_db = strdup(rpp2app(".mpddb"));
|
|
||||||
else directory_db = strdup(options.dbFile);
|
|
||||||
|
|
||||||
if(options.createDB>0 || readDirectoryDB()<0) {
|
|
||||||
if(options.createDB<0) {
|
|
||||||
ERROR("can't open db file and using \"--no-create-db\""
|
ERROR("can't open db file and using \"--no-create-db\""
|
||||||
" command line option\n");
|
" command line option\n");
|
||||||
ERROR("try running \"%s --only-create-db\"\n",
|
ERROR("try running \"%s --create-db\"\n",
|
||||||
argv[0]);
|
argv0);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
initMp3Directory();
|
initMp3Directory();
|
||||||
@ -332,21 +321,16 @@ int main(int argc, char * argv[]) {
|
|||||||
ERROR("problem opening db for reading or writing\n");
|
ERROR("problem opening db for reading or writing\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if(options.createDB) exit(EXIT_SUCCESS);
|
if(options->createDB) exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
if(options.updateDB) {
|
if(options->updateDB) {
|
||||||
if(updateMp3Directory(stderr)<0) exit(EXIT_FAILURE);
|
if(updateMp3Directory(stderr)<0) exit(EXIT_FAILURE);
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
initCommands();
|
void daemonize(Options * options) {
|
||||||
initAudioConfig();
|
if(options->daemon) {
|
||||||
initAudioDriver();
|
|
||||||
initPlayerData();
|
|
||||||
initVolume();
|
|
||||||
initInterfaces();
|
|
||||||
|
|
||||||
if(options.daemon) {
|
|
||||||
int pid;
|
int pid;
|
||||||
|
|
||||||
fflush(NULL);
|
fflush(NULL);
|
||||||
@ -375,12 +359,10 @@ int main(int argc, char * argv[]) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(options.stdOutput) {
|
void setupLogOutput(Options * options, FILE * out, FILE * err) {
|
||||||
fclose(out);
|
if(!options->stdOutput) {
|
||||||
fclose(err);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
fflush(NULL);
|
fflush(NULL);
|
||||||
|
|
||||||
if(dup2(fileno(out),STDOUT_FILENO)<0) {
|
if(dup2(fileno(out),STDOUT_FILENO)<0) {
|
||||||
@ -395,7 +377,8 @@ int main(int argc, char * argv[]) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
myfprintfStdLogMode(out,err,options.logFile,options.errorFile);
|
myfprintfStdLogMode(out, err, options->logFile,
|
||||||
|
options->errorFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* lets redirect stdin to dev null as a work around for libao bug */
|
/* lets redirect stdin to dev null as a work around for libao bug */
|
||||||
@ -412,6 +395,51 @@ int main(int argc, char * argv[]) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char * argv[]) {
|
||||||
|
FILE * out;
|
||||||
|
FILE * err;
|
||||||
|
Options options;
|
||||||
|
|
||||||
|
closeAllFDs();
|
||||||
|
|
||||||
|
initConf();
|
||||||
|
|
||||||
|
parseOptions(argc, argv, &options);
|
||||||
|
|
||||||
|
initStats();
|
||||||
|
initLog();
|
||||||
|
|
||||||
|
establishListen(&options);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* lose privileges as early as possible
|
||||||
|
*/
|
||||||
|
changeToUser(&options);
|
||||||
|
|
||||||
|
openLogFiles(&options, &out, &err);
|
||||||
|
|
||||||
|
initPaths(options.playlistDirArg,options.musicDirArg);
|
||||||
|
initPermissions();
|
||||||
|
initReplayGainState();
|
||||||
|
|
||||||
|
initTables();
|
||||||
|
initPlaylist();
|
||||||
|
initInputPlugins();
|
||||||
|
|
||||||
|
openDB(&options, argv[0]);
|
||||||
|
|
||||||
|
initCommands();
|
||||||
|
initAudioConfig();
|
||||||
|
initAudioDriver();
|
||||||
|
initPlayerData();
|
||||||
|
initVolume();
|
||||||
|
initInterfaces();
|
||||||
|
|
||||||
|
daemonize(&options);
|
||||||
|
|
||||||
|
setupLogOutput(&options, out, err);
|
||||||
|
|
||||||
openVolumeDevice();
|
openVolumeDevice();
|
||||||
initSigHandlers();
|
initSigHandlers();
|
||||||
|
Loading…
Reference in New Issue
Block a user