+/*\r
+ Try multiple times to read from a file.\r
+ Returns: 0 on success.\r
+ 1 on non-fatal error.\r
+ -1 on fatal error.\r
+*/\r
+static int try_read(logger_t *logger, void *address, unsigned long bufsize, unsigned long *in, int *complained) {\r
+ int ret = 1;\r
+ unsigned long error;\r
+ for (int tries = 0; tries < 5; tries++) {\r
+ if (ReadFile(logger->read_handle, address, bufsize, in, 0)) return 0;\r
+\r
+ error = GetLastError();\r
+ switch (error) {\r
+ /* Other end closed the pipe. */\r
+ case ERROR_BROKEN_PIPE:\r
+ ret = -1;\r
+ goto complain_read;\r
+\r
+ /* Couldn't lock the buffer. */\r
+ case ERROR_NOT_ENOUGH_QUOTA:\r
+ Sleep(2000 + tries * 3000);\r
+ ret = 1;\r
+ continue;\r
+\r
+ /* Write was cancelled by the other end. */\r
+ case ERROR_OPERATION_ABORTED:\r
+ ret = 1;\r
+ goto complain_read;\r
+\r
+ default:\r
+ ret = -1;\r
+ }\r
+ }\r
+\r
+complain_read:\r
+ /* Ignore the error if we've been requested to exit anyway. */\r
+ if (*logger->rotate_online != NSSM_ROTATE_ONLINE) return ret;\r
+ if (! (*complained & COMPLAINED_READ)) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_READFILE_FAILED, logger->service_name, logger->path, error_string(error), 0);\r
+ *complained |= COMPLAINED_READ;\r
+ return ret;\r
+}\r
+\r
+/*\r
+ Try multiple times to write to a file.\r
+ Returns: 0 on success.\r
+ 1 on non-fatal error.\r
+ -1 on fatal error.\r
+*/\r
+static int try_write(logger_t *logger, void *address, unsigned long bufsize, unsigned long *out, int *complained) {\r
+ int ret = 1;\r
+ unsigned long error;\r
+ for (int tries = 0; tries < 5; tries++) {\r
+ if (WriteFile(logger->write_handle, address, bufsize, out, 0)) return 0;\r
+\r
+ error = GetLastError();\r
+ if (error == ERROR_IO_PENDING) {\r
+ /* Operation was successful pending flush to disk. */\r
+ return 0;\r
+ }\r
+\r
+ switch (error) {\r
+ /* Other end closed the pipe. */\r
+ case ERROR_BROKEN_PIPE:\r
+ ret = -1;\r
+ goto complain_write;\r
+\r
+ /* Couldn't lock the buffer. */\r
+ case ERROR_NOT_ENOUGH_QUOTA:\r
+ /* Out of disk space. */\r
+ case ERROR_DISK_FULL:\r
+ Sleep(2000 + tries * 3000);\r
+ ret = 1;\r
+ continue;\r
+\r
+ default:\r
+ /* We'll lose this line but try to read and write subsequent ones. */\r
+ ret = 1;\r
+ }\r
+ }\r
+\r
+complain_write:\r
+ if (! (*complained & COMPLAINED_WRITE)) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_WRITEFILE_FAILED, logger->service_name, logger->path, error_string(error), 0);\r
+ *complained |= COMPLAINED_WRITE;\r
+ return ret;\r