state.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Select;
  5. use File::Basename;
  6. use File::Temp qw( tempfile );
  7. use Sys::Hostname;
  8. use IPC::System::Simple qw( system capture );
  9. # Local modules
  10. use Cwd qw(abs_path);
  11. use File::Basename qw(dirname);
  12. use lib dirname(abs_path($0));
  13. use DMX;
  14. # Prototypes
  15. sub mtime($);
  16. # User config
  17. my %DISPLAY_DEVS = ('TV' => 1, 'PROJECTOR' => 1);
  18. my $DISPLAY = undef();
  19. my $STATE_TIMEOUT = 180;
  20. my $EXISTS_TIMEOUT = 900;
  21. my $START_DELAY = 15;
  22. my %MON_FILES = ();
  23. # Host-specific config
  24. # This is necessary to avoid contention on shared disks
  25. my $HOST = Sys::Hostname::hostname();
  26. if ($HOST =~ /loki/i) {
  27. # Ensure the media path is available before we get going
  28. my $MEDIA_PATH = undef();
  29. {
  30. my $mount = $ENV{'HOME'} . '/bin/video/mediaPath';
  31. $MEDIA_PATH = capture($mount);
  32. }
  33. if (!$MEDIA_PATH || !-d $MEDIA_PATH) {
  34. die("Unable to access media path\n");
  35. }
  36. # Garage door opener
  37. $MON_FILES{ $MEDIA_PATH . '/DMX/cmd/GARAGE_CMD' } = 'EXISTS-VALUE-CLEAR';
  38. }
  39. # Available state files
  40. {
  41. # Plex
  42. $MON_FILES{'PLEX'} = 'NONE';
  43. $MON_FILES{'GUI'} = 'NONE';
  44. $MON_FILES{'PLAYING'} = 'PLAYING';
  45. # RiffTrax
  46. $MON_FILES{'RIFF'} = 'VALUE-NOUPDATE';
  47. # Motion detection
  48. $MON_FILES{'MOTION'} = 'MTIME';
  49. $MON_FILES{'MOTION_STAIRS'} = 'MTIME-NOUPDATE';
  50. $MON_FILES{'NO_MOTION'} = 'EXISTS-NOUPDATE';
  51. # Projector
  52. $MON_FILES{'PROJECTOR'} = 'STATUS';
  53. $MON_FILES{'PROJECTOR_COLOR'} = 'VALUE-NOUPDATE';
  54. $MON_FILES{'PROJECTOR_INPUT'} = 'VALUE-NOUPDATE';
  55. $MON_FILES{'PROJECTOR_LAMP'} = 'VALUE-NOUPDATE';
  56. # Amplifier
  57. $MON_FILES{'AMPLIFIER'} = 'STATUS';
  58. $MON_FILES{'AMPLIFIER_VOL'} = 'VALUE-NOUPDATE';
  59. $MON_FILES{'AMPLIFIER_MODE'} = 'VALUE-NOUPDATE';
  60. $MON_FILES{'AMPLIFIER_INPUT'} = 'VALUE-NOUPDATE';
  61. $MON_FILES{'AUDIO_AMP'} = 'EXISTS-ON';
  62. $MON_FILES{'STEREO_CMD'} = 'EXISTS-ON';
  63. # A/V Effects
  64. $MON_FILES{'LIGHTS'} = 'EXISTS-TIMEOUT';
  65. $MON_FILES{'BRIGHT'} = 'EXISTS-TIMEOUT';
  66. $MON_FILES{'RAVE'} = 'EXISTS';
  67. $MON_FILES{'EFFECT'} = 'EXISTS';
  68. # Equipment
  69. $MON_FILES{'FAN_CMD'} = 'EXISTS-ON';
  70. # OS State
  71. $MON_FILES{'FRONT_APP'} = 'VALUE-NOUPDATE';
  72. $MON_FILES{'COLOR'} = 'VALUE-NOUPDATE';
  73. $MON_FILES{'AUDIO'} = 'VALUE-NOUPDATE';
  74. $MON_FILES{'AUDIO_STATE'} = 'VALUE-NOUPDATE';
  75. # Light State
  76. $MON_FILES{'STAIRS'} = 'VALUE-NOUPDATE';
  77. # TV
  78. $MON_FILES{'TV'} = 'STATUS';
  79. $MON_FILES{'TV_VOL'} = 'VALUE';
  80. }
  81. my %EXTRAS = (
  82. 'PLAYING' => {
  83. 'URL' => qr/^file:(.+)$/m,
  84. 'YEAR' => qr/^year:(\d+)/m,
  85. 'SERIES' => qr/^showtitle:(.+)$/m,
  86. 'SEASON' => qr/^season:(\d+)/m,
  87. 'EPISODE' => qr/^episode:(\d+)/m,
  88. 'TITLE' => qr/^title:(.+)$/m,
  89. 'LENGTH' => qr/^duration:(\d+)/m,
  90. 'POSITION' => qr/^time:(\d+(?:\.\d+)?)/m,
  91. 'TYPE' => qr/^type:(.+)$/m,
  92. 'IMAGE' => qr/^fanart:(.+)$/m,
  93. 'THUMB' => qr/^thumbnail:(.+)$/m,
  94. 'ALBUM' => qr/^album:(.+)$/m,
  95. 'ARTIST' => qr/^artist:(.+)$/m,
  96. 'SELECTION' => qr/^selection:(.+)$/m,
  97. 'WINDOW' => qr/^window:(.+)$/m,
  98. 'WINDOWID' => qr/^windowid:(\d+)/m,
  99. }
  100. );
  101. # App config
  102. my $SOCK_TIMEOUT = 5;
  103. my $DATA_DIR = DMX::dataDir();
  104. my $CMD_FILE = 'STATE';
  105. my $MAX_CMD_LEN = 4096;
  106. my $RESET_CMD = $ENV{'HOME'} . '/bin/video/dmx/reset.sh';
  107. my $PUSH_TIMEOUT = 20;
  108. # Debug
  109. my $DEBUG = 0;
  110. if ($ENV{'DEBUG'}) {
  111. $DEBUG = 1;
  112. }
  113. # Command-line arguments
  114. my ($DELAY) = @ARGV;
  115. if (!$DELAY) {
  116. $DELAY = 0.5;
  117. }
  118. # Socket init
  119. my $select = DMX::stateSocket($CMD_FILE);
  120. # Add the extras to the main file list
  121. foreach my $file (keys(%EXTRAS)) {
  122. if (exists($MON_FILES{$file})) {
  123. foreach my $extra (keys(%{ $EXTRAS{$file} })) {
  124. $MON_FILES{ $file . '_' . $extra } = 'NONE';
  125. }
  126. }
  127. }
  128. # Init the file tracking structure
  129. my %files = ();
  130. foreach my $name (keys(%MON_FILES)) {
  131. my %file = (
  132. 'name' => basename($name),
  133. 'type' => $MON_FILES{$name},
  134. 'path' => $DATA_DIR . $name,
  135. 'update' => 0,
  136. 'value' => 0,
  137. 'last' => 0,
  138. );
  139. # Allow absolute paths to override the $DATA_DIR path
  140. if ($name =~ /^\//) {
  141. $file{'path'} = $name;
  142. }
  143. # Record the directory name for folder monitoring
  144. $file{'dir'} = dirname($file{'path'});
  145. # Set the attribute bits, for use in later update handling logic
  146. my %attr = (
  147. 'available' => 0,
  148. 'status' => 0,
  149. 'exists' => 0,
  150. 'value' => 0,
  151. 'clear' => 0,
  152. 'clear_off' => 0,
  153. 'mtime' => 0,
  154. 'playing' => 0,
  155. 'no_update' => 0,
  156. );
  157. if ($file{'type'} =~ /\bSTATUS\b/i) {
  158. $attr{'status'} = 1;
  159. }
  160. if ($file{'type'} =~ /\bEXISTS\b/i) {
  161. $attr{'exists'} = 1;
  162. }
  163. if ($file{'type'} =~ /\bVALUE\b/i) {
  164. $attr{'value'} = 1;
  165. }
  166. if ($file{'type'} =~ /\bCLEAR\b/i) {
  167. $attr{'clear'} = 1;
  168. }
  169. if ($file{'type'} =~ /\bON\b/i) {
  170. $attr{'clear_off'} = 1;
  171. }
  172. if ($file{'type'} =~ /\bTIMEOUT\b/i) {
  173. $attr{'clear_timeout'} = 1;
  174. }
  175. if ($file{'type'} =~ /\bMTIME\b/i) {
  176. $attr{'mtime'} = 1;
  177. }
  178. if ($file{'type'} =~ /\bPLAYING\b/i) {
  179. $attr{'playing'} = 1;
  180. }
  181. if ($file{'type'} =~ /\bNOUPDATE\b/i) {
  182. $attr{'no_update'} = 1;
  183. }
  184. # Cross-match some data types for easy of use
  185. if ($attr{'value'} || $attr{'playing'}) {
  186. $attr{'status'} = 1;
  187. }
  188. if ($attr{'status'} || $attr{'exists'}) {
  189. $attr{'mtime'} = 1;
  190. }
  191. # Push ATTR into the file hash
  192. $file{'attr'} = \%attr;
  193. # Push FILE into the files hash
  194. $files{$name} = \%file;
  195. }
  196. # Delete any existing input files, to ensure our state is reset
  197. foreach my $file (values(%files)) {
  198. if (-e $file->{'path'}) {
  199. if ($DEBUG) {
  200. print STDERR 'Deleting ' . $file->{'name'} . ': ' . $file->{'path'} . "\n";
  201. }
  202. unlink($file->{'path'});
  203. }
  204. }
  205. # Reset all dependents at starts
  206. system($RESET_CMD);
  207. # Subscribers
  208. my @subscribers = ();
  209. # State
  210. my $state = 'INIT';
  211. my $stateLast = $state;
  212. my $status = '';
  213. my $statusLast = $status;
  214. my $updateLast = 0;
  215. my $timeSinceUpdate = 0;
  216. my $startTime = time() + $START_DELAY;
  217. my $pushLast = 0;
  218. # Loop forever
  219. while (1) {
  220. # Provide a method to force updates even when the state does not change
  221. my $update = 0;
  222. # Check for queued commands
  223. my @ready_clients = $select->can_read($DELAY);
  224. foreach my $fh (@ready_clients) {
  225. # Grab the inbound text
  226. my $path = undef();
  227. $fh->recv($path, $MAX_CMD_LEN);
  228. $path =~ s/^\s+//;
  229. $path =~ s/\s+$//;
  230. $path =~ s/\W/_/g;
  231. if ($DEBUG) {
  232. print STDERR 'Got path: ' . $path . "\n";
  233. }
  234. # Open the new socket
  235. my $sub = eval { DMX::clientSock($path); };
  236. if (!$sub) {
  237. print STDERR 'Unable to open socket: ' . $path . ": ${@}\n";
  238. next;
  239. }
  240. # Add the socket to our subscriber list
  241. my %tmp = (
  242. 'path' => $path,
  243. 'socket' => $sub,
  244. );
  245. push(@subscribers, \%tmp);
  246. # Force an update
  247. $update = 1;
  248. }
  249. # Use opendir/readdir in each folder to ensure fresh results
  250. my %folders = ();
  251. foreach my $file (values(%files)) {
  252. if (!$folders{ $file->{'dir'} }) {
  253. my $dh = undef();
  254. opendir($dh, $file->{'dir'})
  255. or die('Unable to open directory: ' . $file->{'dir'});
  256. if ($DEBUG) {
  257. print STDERR 'Refreshed folder: ' . $file->{'dir'} . "\n";
  258. }
  259. my @files = readdir($dh);
  260. closedir($dh);
  261. $folders{ $file->{'dir'} } = 1;
  262. }
  263. }
  264. # Monitor files of all types
  265. foreach my $file (values(%files)) {
  266. # Skip "NONE" files -- they are data stores handled in other actions
  267. if ($file->{'type'} eq 'NONE') {
  268. next;
  269. }
  270. # Always record the previous status and clear the new one
  271. $file->{'last'} = $file->{'value'};
  272. $file->{'value'} = 0;
  273. # Track available/unavailable status in each cycle
  274. {
  275. my $wasAvailable = $file->{'attr'}->{'available'};
  276. $file->{'attr'}->{'available'} = -r $file->{'path'} ? 1 : 0;
  277. if ($wasAvailable != $file->{'attr'}->{'available'}) {
  278. # Reset and extras for this file
  279. if (exists($EXTRAS{ $file->{'name'} })) {
  280. foreach my $extra (keys(%{ $EXTRAS{ $file->{'name'} } })) {
  281. my $name = $file->{'name'} . '_' . $extra;
  282. $files{$name}{'last'} = $files{$name}{'value'};
  283. $files{$name}{'value'} = 0;
  284. }
  285. }
  286. # Determine which display device we have (if any)
  287. if (exists($DISPLAY_DEVS{ $file->{'name'} })) {
  288. $DISPLAY = $file->{'name'};
  289. if ($DEBUG) {
  290. print STDERR 'Selecting display device: ' . $DISPLAY . "\n";
  291. }
  292. }
  293. if ($DEBUG) {
  294. if ($file->{'attr'}->{'available'}) {
  295. print STDERR 'Added file ' . $file->{'name'} . ': ' . $file->{'path'} . "\n";
  296. } else {
  297. print STDERR 'Dropped file ' . $file->{'name'} . ': ' . $file->{'path'} . "\n";
  298. }
  299. }
  300. }
  301. }
  302. # Skip unavailable files
  303. if (!$file->{'attr'}->{'available'}) {
  304. next;
  305. }
  306. # Record the last update for MTIME files
  307. if ($file->{'attr'}->{'mtime'}) {
  308. my $mtime = mtime($file->{'path'});
  309. # All EXISTS files have MTIME set, and mtime returns 0 if the file does not exist, so we can overload this check
  310. if ($file->{'attr'}->{'exists'}) {
  311. if ($mtime > 0) {
  312. $file->{'update'} = $mtime;
  313. $file->{'value'} = 1;
  314. } elsif ($file->{'last'}) {
  315. $file->{'update'} = time();
  316. }
  317. } else {
  318. $file->{'update'} = $mtime;
  319. }
  320. if ($DEBUG) {
  321. print STDERR 'Mtime: ' . $file->{'name'} . ': ' . $file->{'value'} . ':' . $file->{'update'} . "\n";
  322. }
  323. }
  324. # Grab the state from STATUS files
  325. if ($file->{'attr'}->{'status'}) {
  326. my $text = '';
  327. {
  328. if (!open(my $fh, $file->{'path'})) {
  329. warn('Unable to open ' . $file->{'path'} . "\n");
  330. } else {
  331. local $/;
  332. $text = <$fh>;
  333. close($fh);
  334. }
  335. }
  336. # Parse the value for the file
  337. if ($file->{'attr'}->{'value'}) {
  338. $text =~ s/\n$//;
  339. $file->{'value'} = $text;
  340. } elsif ($file->{'attr'}->{'playing'}) {
  341. if ($text =~ /^playing:1/m) {
  342. $file->{'value'} = 1;
  343. }
  344. } else {
  345. if ($text =~ /1/) {
  346. $file->{'value'} = 1;
  347. }
  348. }
  349. if ($DEBUG) {
  350. print STDERR 'Status: ' . $file->{'name'} . ': ' . $file->{'value'} . "\n";
  351. }
  352. # Handle extras, if any
  353. if (exists($EXTRAS{ $file->{'name'} })) {
  354. foreach my $extra (keys(%{ $EXTRAS{ $file->{'name'} } })) {
  355. my $name = $file->{'name'} . '_' . $extra;
  356. $files{$name}{'last'} = $files{$name}{'value'};
  357. $files{$name}{'value'} = 0;
  358. $files{$name}{'update'} = $file->{'update'};
  359. if ($text =~ $EXTRAS{ $file->{'name'} }{$extra}) {
  360. $files{$name}{'value'} = $1;
  361. }
  362. if ($DEBUG) {
  363. print STDERR 'Status (extra): ' . $name . ': ' . $files{$name}{'value'} . "\n";
  364. }
  365. }
  366. }
  367. }
  368. }
  369. # Simulate the old GUI indicator
  370. {
  371. my $gui = 0;
  372. if (exists($files{'PLAYING_WINDOW'}) && $files{'PLAYING_WINDOW'}->{'value'} ne 'Fullscreen video') {
  373. $gui = 1;
  374. }
  375. $files{'GUI'}->{'value'} = $gui;
  376. }
  377. if (exists($files{'PLAYING'})) {
  378. $files{'GUI'}->{'update'} = $files{'PLAYING'}->{'update'};
  379. }
  380. # Ignore the MOTION file if NO_MOTION is set
  381. if (exists($files{'NO_MOTION'}) && $files{'NO_MOTION'}->{'value'}) {
  382. if (exists($files{'MOTION'})) {
  383. $files{'MOTION'}->{'value'} = 0;
  384. $files{'MOTION'}->{'update'} = 0;
  385. }
  386. }
  387. # Set the global update timestamp, excluding files marked NOUPDATE
  388. foreach my $file (values(%files)) {
  389. if ( $file->{'update'} > $updateLast
  390. && !$file->{'attr'}->{'no_update'}
  391. && $file->{'update'} > $startTime)
  392. {
  393. $updateLast = $file->{'update'};
  394. }
  395. }
  396. $timeSinceUpdate = time() - $updateLast;
  397. # Calculate the PLEX state
  398. $files{'PLEX'}->{'last'} = $files{'PLEX'}->{'value'};
  399. $files{'PLEX'}->{'value'} = 0;
  400. if (exists($files{'FRONT_APP'})) {
  401. if ( $files{'FRONT_APP'}->{'value'} =~ 'com.plexapp.plexhometheater'
  402. || $files{'FRONT_APP'}->{'value'} eq 'com.apple.ScreenSaver.Engine')
  403. {
  404. $files{'PLEX'}->{'value'} = 1;
  405. }
  406. }
  407. if ($files{'PLEX'}->{'last'} != $files{'PLEX'}->{'value'}) {
  408. $files{'PLEX'}->{'update'} = time();
  409. }
  410. # Determine some intermediate state data
  411. my $playing = 0;
  412. if (exists($files{'PLAYING'}) && $files{'PLAYING'}->{'value'}) {
  413. $playing = 1;
  414. }
  415. my $video = 0;
  416. if (exists($files{'PLAYING_TYPE'}) && $files{'PLAYING_TYPE'}->{'value'} ne 'audio') {
  417. $video = 1;
  418. } elsif (!exists($files{'PLAYING_TYPE'})) {
  419. $video = 1;
  420. }
  421. # Non-plex apps are always "playing" and "video"
  422. if (exists($files{'PLEX'}) && !$files{'PLEX'}->{'value'}) {
  423. $playing = 1;
  424. $video = 1;
  425. }
  426. # Calculate the new state
  427. $stateLast = $state;
  428. if (!defined($DISPLAY) || !exists($files{$DISPLAY})) {
  429. # If there's no display the state is always PLAY or PAUSE, as indicated by the master
  430. if ($playing) {
  431. $state = 'PLAY';
  432. } else {
  433. $state = 'PAUSE';
  434. }
  435. } elsif ($files{$DISPLAY}->{'value'}) {
  436. # If a display exists and is on:
  437. # PLAY when video is active and playing
  438. # PAUSE any other time (including when audio is active)
  439. if ($playing && $video) {
  440. $state = 'PLAY';
  441. } else {
  442. $state = 'PAUSE';
  443. }
  444. } else {
  445. # If the display exists but is off, check the timeouts to determine MOTION vs. OFF
  446. if ($timeSinceUpdate > $STATE_TIMEOUT) {
  447. $state = 'OFF';
  448. } elsif ($timeSinceUpdate < $STATE_TIMEOUT) {
  449. $state = 'MOTION';
  450. }
  451. }
  452. if ($state eq 'INIT') {
  453. $state = 'OFF';
  454. }
  455. # Clear EXISTS_ON files when the main state is "OFF" (and before we append their status)
  456. foreach my $file (values(%files)) {
  457. if ( $file->{'attr'}->{'clear_off'}
  458. && $file->{'value'}
  459. && $state eq 'OFF')
  460. {
  461. unlink($file->{'path'});
  462. $file->{'status'} = 0;
  463. if ($DEBUG) {
  464. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  465. }
  466. }
  467. }
  468. # Clear EXISTS_TIMEOUT files when the main state is "OFF" and EXISTS_TIMEOUT has expired (and before we append their status)
  469. foreach my $file (values(%files)) {
  470. if ( $file->{'attr'}->{'clear_timeout'}
  471. && $file->{'value'}
  472. && $state eq 'OFF'
  473. && $timeSinceUpdate > $EXISTS_TIMEOUT)
  474. {
  475. unlink($file->{'path'});
  476. $file->{'status'} = 0;
  477. if ($DEBUG) {
  478. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  479. }
  480. }
  481. }
  482. # Calculate the new status
  483. $statusLast = $status;
  484. {
  485. my @statTime = ();
  486. foreach my $file (values(%files)) {
  487. my $text = $file->{'value'};
  488. $text =~ s/\s/ /g;
  489. $text =~ s/\|/-/g;
  490. push(@statTime, $file->{'name'} . '|' . $text . '|' . $file->{'update'});
  491. }
  492. $status = "\n" . join("\n", @statTime);
  493. }
  494. # Clear EXISTS_CLEAR files immediately (but after we append their status)
  495. foreach my $file (values(%files)) {
  496. if ($file->{'attr'}->{'clear'} && $file->{'value'}) {
  497. unlink($file->{'path'});
  498. $file->{'status'} = 0;
  499. if ($DEBUG) {
  500. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  501. }
  502. }
  503. }
  504. # Force updates on a periodic basis
  505. if (time() - $pushLast > $PUSH_TIMEOUT) {
  506. $update = 1;
  507. }
  508. # Update on any status change
  509. foreach my $file (values(%files)) {
  510. if ($file->{'value'} ne $file->{'last'}) {
  511. $update = 1;
  512. last;
  513. }
  514. }
  515. # Force updates on any state change
  516. if ($stateLast ne $state || $statusLast ne $status) {
  517. if ($DEBUG) {
  518. print STDERR 'State change: ' . $stateLast . $statusLast . ' => ' . $state . $status . "\n";
  519. }
  520. $update = 1;
  521. }
  522. # Push the update
  523. if ($update) {
  524. # Note the push
  525. $pushLast = time();
  526. # Send notifications to all subscribers
  527. foreach my $sub (@subscribers) {
  528. # Drop subscribers that are not available
  529. if (!eval { $sub->{'socket'}->send($state . $status) }) {
  530. print STDERR 'Dropping bad socket from subscriber list: ' . $sub->{'path'} . "\n";
  531. my @new_subscribers = ();
  532. foreach my $new (@subscribers) {
  533. if ($new->{'socket'} eq $sub->{'socket'}) {
  534. next;
  535. }
  536. push(@new_subscribers, $new);
  537. }
  538. @subscribers = @new_subscribers;
  539. }
  540. }
  541. # Save the state and value to disk
  542. my ($fh, $tmp) = tempfile($DATA_DIR . 'STATE.XXXXXXXX', 'UNLINK' => 0);
  543. print $fh $state . "\n";
  544. close($fh);
  545. rename($tmp, $DATA_DIR . 'STATE');
  546. }
  547. }
  548. sub mtime($) {
  549. my ($file) = @_;
  550. my $mtime = 0;
  551. if (-r $file) {
  552. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  553. }
  554. return $mtime;
  555. }