- /* Wait for the MicroB browserd lockfile to be created */
- log_msg("Waiting for browserd lockfile to be created\n");
- memset(buf, '\0', 256);
- /* read() blocks until there are events to be read */
- while ((bytes_read = read(fd, buf, 255)) > 0) {
- pos = buf;
- /* Loop until we see the event we're looking for
- or until all the events are processed */
- while (pos && (pos-buf) < bytes_read) {
- event = (struct inotify_event *)pos;
- len = sizeof(struct inotify_event) + event->len;
- if (!strcmp(MICROB_LOCKFILE, event->name)) {
- /* Lockfile created */
- pos = NULL;
+ if (!pid)
+ /* If we didn't start the MicroB browser process ourselves, try
+ to get the PID of the browserd from the lockfile */
+ browserd_pid = get_browserd_pid(microb_lockfile);
+ else
+ browserd_pid = 0;
+
+ /* If getting the lockfile PID failed, or the lockfile PID doesn't
+ exist, assume that we have a stale lockfile and wait for the new
+ browserd lockfile to be created */
+ if (browserd_pid <= 0 || kill(browserd_pid, 0) == ESRCH) {
+ log_msg("Waiting for browserd lockfile to be created\n");
+ memset(buf, '\0', 256);
+ /* read() blocks until there are events to be read */
+ while ((bytes_read = read(fd, buf, 255)) > 0) {
+ pos = buf;
+ /* Loop until we see the event we're looking for
+ or until all the events are processed */
+ while (pos && (pos-buf) < bytes_read) {
+ event = (struct inotify_event *)pos;
+ len = sizeof(struct inotify_event) + event->len;
+ if (!strcmp(MICROB_LOCKFILE, event->name)) {
+ /* Lockfile created */
+ pos = NULL;
+ break;
+ } else if ((pos-buf) + len < bytes_read)
+ /* More events to process */
+ pos += len;
+ else
+ /* All events processed */
+ break;
+ }
+ if (!pos)
+ /* Event found, stop looking */