1 /*
2  * Copyright (c) 2002, 2003 MATPOCKuH.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <libgen.h>
28 #include <sysexits.h>
29 #include <stdlib.h>
30 
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34 
35 #include "settings.h"
36 #include "logger.h"
37 #include "acd.h"
38 #include "auto_cd.h"
39 #include "version.h"
40 
41 const char *progname = "autocd";
42 void
usage(char * argv[])43 usage(char *argv[])
44 {
45     logger("%s\n\
46 Usage: %s [-hc] [-d device] [-p mountpoint] [-f fstype] [-o options]\n\
47 \t[-s socket] [-u user] [-g group] [-m mode]\n\
48   -h\t\t- this help\n\
49   -c\t\t- don't close devices while playing is in progress\n\
50   -d device\t- device for monitoring by autocd\n\
51   -p mountpoint\t- mount point for device\n\
52   -f fstype\t- file system type for device\n\
53   -o options\t- mount options for device\n\
54   -s socket\t- path to a control socket\n\
55   -u user\t- socket owner\n\
56   -g group\t- socket group\n\
57   -m mode\t- socket mode\n",
58 	ver_getversionstring(), basename(argv[0]));
59 
60     exit(EX_USAGE);
61 }
62 
63 int
main(int argc,char * argv[])64 main(int argc, char *argv[])
65 {
66     int ch, devices_selected = 0, devices_ok = 0, last_device = 0;
67 
68     autocd_init();
69 
70     while((ch = getopt(argc, argv, "hcd:p:f:o:s:u:g:m:")) != -1) {
71 	switch(ch) {
72 	case 'c':
73 	    autocd_noclose();
74 	    break;
75 
76 	case 'd':
77 	    devices_selected = 1;
78 	    if(!autocd_initdevice(optarg))
79 		devices_ok = last_device = 1;
80 	    else
81 		last_device = 0;
82 
83 	    break;
84 
85 	case 'p':
86 	case 'f':
87 	case 'o':
88 	    if(devices_selected) {
89 		if(!last_device) {
90 		    logger("Last device is not usable. -%c ignored\n", ch);
91 		    break;
92 		}
93 	    } else {
94 		devices_selected = 1;
95 		if(autocd_initdevice(CDDEV))
96 		    break;
97 
98 		devices_ok = 1;
99 	    }
100 
101 	    switch(ch) {
102 	    case 'p':
103 		autocd_set_mnt_point(optarg);
104 		break;
105 
106 	    case 'f':
107 		autocd_set_mnt_fstype(optarg);
108 		break;
109 
110 	    case 'o':
111 		autocd_set_mnt_opts(optarg);
112 		break;
113 	    }
114 
115 	    break;
116 
117 	case 's':
118 	    acd_setsocketpath(optarg);
119 	    break;
120 
121 	case 'u':
122 	    if(autocd_setsocketowner(optarg))
123 		exit(EX_NOUSER);
124 	    break;
125 
126 	case 'g':
127 	    if(autocd_setsocketgroup(optarg))
128 		exit(EX_NOUSER);
129 	    break;
130 
131 	case 'm':
132 	    if(autocd_setsocketmode(optarg))
133 		exit(EX_USAGE);
134 	    break;
135 
136 	case 'h':
137 	default:
138 		usage(argv);
139 	}
140     }
141 
142     if(argc != optind)
143 	usage(argv);
144 
145     if(!devices_selected) {
146 	if(!autocd_initdevice(CDDEV))
147 	    devices_ok = 1;
148     }
149 
150     if(!devices_ok) {
151 	logger("No usable devices. Exiting.\n");
152         exit(EX_USAGE);
153     }
154 
155     if(autocd_initsocket())
156 	exit(EX_NOPERM);
157 
158     if(autocd_daemon())
159 	exit(EX_OSERR);
160 
161     return 0;
162 }
163