chiark / gitweb /
choose: Select tracks uniformly at random.
[disorder] / server / choose.c
1 /*
2  * This file is part of DisOrder 
3  * Copyright (C) 2008 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 /** @file choose.c
21  * @brief Random track chooser
22  *
23  * Picks a track at random and writes it to standard output.  If for
24  * any reason no track can be picked - even a trivial reason like a
25  * deadlock - it just exits and expects the server to try again.
26  */
27
28 #include <config.h>
29 #include "types.h"
30
31 #include <getopt.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <db.h>
35 #include <locale.h>
36 #include <errno.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <pcre.h>
40 #include <string.h>
41 #include <fcntl.h>
42 #include <syslog.h>
43 #include <time.h>
44
45 #include "configuration.h"
46 #include "log.h"
47 #include "defs.h"
48 #include "mem.h"
49 #include "kvp.h"
50 #include "syscalls.h"
51 #include "printf.h"
52 #include "trackdb.h"
53 #include "trackdb-int.h"
54 #include "version.h"
55 #include "trackname.h"
56 #include "queue.h"
57 #include "server-queue.h"
58
59 static DB_TXN *global_tid;
60
61 static const struct option options[] = {
62   { "help", no_argument, 0, 'h' },
63   { "version", no_argument, 0, 'V' },
64   { "config", required_argument, 0, 'c' },
65   { "debug", no_argument, 0, 'd' },
66   { "no-debug", no_argument, 0, 'D' },
67   { "syslog", no_argument, 0, 's' },
68   { "no-syslog", no_argument, 0, 'S' },
69   { 0, 0, 0, 0 }
70 };
71
72 /* display usage message and terminate */
73 static void help(void) {
74   xprintf("Usage:\n"
75           "  disorder-choose [OPTIONS]\n"
76           "Options:\n"
77           "  --help, -h              Display usage message\n"
78           "  --version, -V           Display version number\n"
79           "  --config PATH, -c PATH  Set configuration file\n"
80           "  --debug, -d             Turn on debugging\n"
81           "  --[no-]syslog           Enable/disable logging to syslog\n"
82           "\n"
83           "Track choose for DisOrder.  Not intended to be run\n"
84           "directly.\n");
85   xfclose(stdout);
86   exit(0);
87 }
88
89 /** @brief Weighted track record */
90 struct weighted_track {
91   /** @brief Next track in the list */
92   struct weighted_track *next;
93   /** @brief Track name */
94   const char *track;
95   /** @brief Weight for this track (always positive) */
96   unsigned long weight;
97 };
98
99 /** @brief List of tracks with nonzero weight */
100 static struct weighted_track *tracks;
101
102 /** @brief Sum of all weights */
103 static unsigned long long total_weight;
104
105 /** @brief Count of tracks */
106 static long ntracks;
107
108 static char **required_tags;
109 static char **prohibited_tags;
110
111 static int queue_contains(const struct queue_entry *head,
112                           const char *track) {
113   const struct queue_entry *q;
114
115   for(q = head->next; q != head; q = q->next)
116     if(!strcmp(q->track, track))
117       return 1;
118   return 0;
119 }
120
121 /** @brief Compute the weight of a track
122  * @param track Track name (UTF-8)
123  * @param data Track data
124  * @param prefs Track preferences
125  * @return Track weight (non-negative)
126  *
127  * Tracks to be excluded entirely are given a weight of 0.
128  */
129 static unsigned long compute_weight(const char *track,
130                                     struct kvp *data,
131                                     struct kvp *prefs) {
132   const char *s;
133   char **track_tags;
134   time_t last, now;
135
136   /* Reject tracks not in any collection (race between edit config and
137    * rescan) */
138   if(!find_track_root(track)) {
139     info("found track not in any collection: %s", track);
140     return 0;
141   }
142
143   /* Reject aliases to avoid giving aliased tracks extra weight */
144   if(kvp_get(data, "_alias_for"))
145     return 0;
146   
147   /* Reject tracks with random play disabled */
148   if((s = kvp_get(prefs, "pick_at_random"))
149      && !strcmp(s, "0"))
150     return 0;
151
152   /* Reject tracks played within the last 8 hours */
153   if((s = kvp_get(prefs, "played_time"))) {
154     last = atoll(s);
155     now = time(0);
156     if(now < last + config->replay_min)
157       return 0;
158   }
159
160   /* Reject tracks currently in the queue or in the recent list */
161   if(queue_contains(&qhead, track)
162      || queue_contains(&phead, track))
163     return 0;
164
165   /* We'll need tags for a number of things */
166   track_tags = parsetags(kvp_get(prefs, "tags"));
167
168   /* Reject tracks with prohibited tags */
169   if(prohibited_tags && tag_intersection(track_tags, prohibited_tags))
170     return 0;
171
172   /* Reject tracks that lack required tags */
173   if(*required_tags && !tag_intersection(track_tags, required_tags))
174     return 0;
175
176   /* Use the configured weight if available */
177   if((s = kvp_get(prefs, "weight"))) {
178     long n;
179     errno = 0;
180
181     n = strtol(s, 0, 10);
182     if((errno == 0 || errno == ERANGE) && n >= 0)
183       return n;
184   }
185   
186   return 90000;
187 }
188
189 /** @brief Called for each track */
190 static int collect_tracks_callback(const char *track,
191                                    struct kvp *data,
192                                    struct kvp *prefs,
193                                    void attribute((unused)) *u,
194                                    DB_TXN attribute((unused)) *tid) {
195   unsigned long weight = compute_weight(track, data, prefs);
196
197   if(weight) {
198     struct weighted_track *const t = xmalloc(sizeof *t);
199
200     /* Clamp weight so that we can fit in billions of tracks when we do
201      * arithmetic in long long */
202     if(weight > 0x7fffffff)
203       weight = 0x7fffffff;
204     t->next = tracks;
205     t->track = track;
206     t->weight = weight;
207     tracks = t;
208     total_weight += weight;
209     ++ntracks;
210   }
211   return 0;
212 }
213
214 /** @brief Pick a random integer uniformly from [0, limit) */
215 static void random_bytes(unsigned char *buf, size_t n) {
216   static int fd = -1;
217   int r;
218
219   if(fd < 0) {
220     if((fd = open("/dev/urandom", O_RDONLY)) < 0)
221       fatal(errno, "opening /dev/urandom");
222   }
223   if((r = read(fd, buf, n)) < 0)
224     fatal(errno, "reading /dev/urandom");
225   if((size_t)r < n)
226     fatal(0, "short read from /dev/urandom");
227 }
228
229 /** @brief Pick a random integer uniformly from [0, limit) */
230 static unsigned long long pick_weight(unsigned long long limit) {
231   unsigned char buf[(sizeof(unsigned long long) * CHAR_BIT + 7)/8], m;
232   unsigned long long t, r, slop;
233   int i, nby, nbi;
234
235   //info("pick_weight: limit = %llu", limit);
236
237   /* First, decide how many bits of output we actually need; do bytes first
238    * (they're quicker) and then bits.
239    *
240    * To speed this up, we could use a binary search if we knew where to
241    * start.  (Note that shifting by ULLONG_BITS or more (if such a constant
242    * existed) is undefined behaviour, so we mustn't do that.)  Figuring out a
243    * start point involves preprocessor and/or autoconf magic.
244    */
245   for (nby = 1, t = (limit - 1) >> 8; t; nby++, t >>= 8)
246     ;
247   nbi = (nby - 1) << 3; t = limit >> nbi;
248   if (t >> 4) { t >>= 4; nbi += 4; }
249   if (t >> 2) { t >>= 2; nbi += 2; }
250   if (t >> 1) { t >>= 1; nbi += 1; }
251   nbi++;
252   //info("nby = %d; nbi = %d", nby, nbi);
253
254   /* Main randomness collection loop.  We read a number of bytes from the
255    * randomness source, and glue them together into an integer (dropping
256    * bits off the top byte as necessary).  Call the result r; we have
257    * 2^{nbi - 1) <= limit < 2^nbi and r < 2^nbi.  If r < limit then we win;
258    * otherwise we try again.  Given the above bounds, we expect fewer than 2
259    * iterations.
260    *
261    * Unfortunately there are subtleties.  In particular, 2^nbi may in fact be
262    * zero due to overflow.  So in fact what we do is compute slop = 2^nbi -
263    * limit > 0; if r < slop then we try again, otherwise r - slop is our
264    * winner.
265    */
266   slop = (2 << (nbi - 1)) - limit;
267   m = nbi & 7 ? (1 << (nbi & 7)) - 1 : 0xff;
268   //info("slop = %llu", slop);
269   //info("m = 0x%02x", m);
270
271   do {
272     /* Actually get some random data. */
273     random_bytes(buf, nby);
274
275     /* Clobber the top byte.  */
276     buf[0] &= m;
277
278     /* Turn it into an integer.  */
279     for (r = 0, i = 0; i < nby; i++)
280       r = (r << 8) | buf[i];
281     //info("r = %llu", r);
282   } while (r < slop);
283
284   return r - slop;
285 }
286
287 /** @brief Pick a track at random and write it to stdout */
288 static void pick_track(void) {
289   long long w;
290   struct weighted_track *t;
291
292   w = pick_weight(total_weight);
293   t = tracks;
294   while(t && w >= t->weight) {
295     w -= t->weight;
296     t = t->next;
297   }
298   if(!t)
299     fatal(0, "ran out of tracks but %lld weighting left", w);
300   xprintf("%s", t->track);
301 }
302
303 int main(int argc, char **argv) {
304   int n, logsyslog = !isatty(2), err;
305   const char *tags;
306   
307   set_progname(argv);
308   mem_init();
309   if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
310   while((n = getopt_long(argc, argv, "hVc:dDSs", options, 0)) >= 0) {
311     switch(n) {
312     case 'h': help();
313     case 'V': version("disorder-choose");
314     case 'c': configfile = optarg; break;
315     case 'd': debugging = 1; break;
316     case 'D': debugging = 0; break;
317     case 'S': logsyslog = 0; break;
318     case 's': logsyslog = 1; break;
319     default: fatal(0, "invalid option");
320     }
321   }
322   if(logsyslog) {
323     openlog(progname, LOG_PID, LOG_DAEMON);
324     log_default = &log_syslog;
325   }
326   if(config_read(0)) fatal(0, "cannot read configuration");
327   /* Find out current queue/recent list */
328   queue_read();
329   recent_read();
330   /* Generate the candidate track list */
331   trackdb_init(TRACKDB_NO_RECOVER);
332   trackdb_open(TRACKDB_NO_UPGRADE|TRACKDB_READ_ONLY);
333   global_tid = trackdb_begin_transaction();
334   if((err = trackdb_get_global_tid("required-tags", global_tid, &tags)))
335     fatal(0, "error getting required-tags: %s", db_strerror(err));
336   required_tags = parsetags(tags);
337   if((err = trackdb_get_global_tid("prohibited-tags", global_tid, &tags)))
338     fatal(0, "error getting prohibited-tags: %s", db_strerror(err));
339   prohibited_tags = parsetags(tags);
340   if(trackdb_scan(0, collect_tracks_callback, 0, global_tid))
341     exit(1);
342   trackdb_commit_transaction(global_tid);
343   trackdb_close();
344   trackdb_deinit();
345   //info("ntracks=%ld total_weight=%lld", ntracks, total_weight);
346   if(!total_weight)
347     fatal(0, "no tracks match random choice criteria");
348   /* Pick a track */
349   pick_track();
350   xfclose(stdout);
351   return 0;
352 }
353
354 /*
355 Local Variables:
356 c-basic-offset:2
357 comment-column:40
358 fill-column:79
359 indent-tabs-mode:nil
360 End:
361 */