pidfile.3 (6b84cd5819ed47f48f9b7ee5530a91b2f067ae34) pidfile.3 (560c4fc142b33d9cf6f202ed76886350444b1992)
1.\" Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
2.\" 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.

--- 91 unchanged lines hidden (view full) ---

100.Dv NULL
101if an error occurs.
102If an error occurs,
103.Va errno
104will be set.
105.Rv -std pidfile_write pidfile_close pidfile_remove
106.Sh EXAMPLES
107The following example shows in which order these functions should be used.
1.\" Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
2.\" 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.

--- 91 unchanged lines hidden (view full) ---

100.Dv NULL
101if an error occurs.
102If an error occurs,
103.Va errno
104will be set.
105.Rv -std pidfile_write pidfile_close pidfile_remove
106.Sh EXAMPLES
107The following example shows in which order these functions should be used.
108Note that it is safe to pass
109.Dv NULL
110to
111.Fn pidfile_write ,
112.Fn pidfile_remove
113and
114.Fn pidfile_close
115functions.
108.Bd -literal
109struct pidfh *pfh;
110pid_t otherpid, childpid;
111
112pfh = pidfile_open("/var/run/daemon.pid", 0600, &otherpid);
113if (pfh == NULL) {
116.Bd -literal
117struct pidfh *pfh;
118pid_t otherpid, childpid;
119
120pfh = pidfile_open("/var/run/daemon.pid", 0600, &otherpid);
121if (pfh == NULL) {
114 if (errno == EEXIST)
115 errx(EXIT_FAILURE, "Daemon already running, pid: %d.", otherpid);
122 if (errno == EEXIST) {
123 errx(EXIT_FAILURE, "Daemon already running, pid: %d.",
124 (int)otherpid);
125 }
116 /* If we cannot create pidfile from other reasons, only warn. */
117 warn("Cannot open or create pidfile");
118}
119
120if (daemon(0, 0) == -1) {
121 warn("Cannot daemonize");
122 pidfile_remove(pfh);
123 exit(EXIT_FAILURE);

--- 8 unchanged lines hidden (view full) ---

132 case -1:
133 syslog(LOG_ERR, "Cannot fork(): %s.", strerror(errno));
134 break;
135 case 0:
136 pidfile_close(pfh);
137 /* Do child work. */
138 break;
139 default:
126 /* If we cannot create pidfile from other reasons, only warn. */
127 warn("Cannot open or create pidfile");
128}
129
130if (daemon(0, 0) == -1) {
131 warn("Cannot daemonize");
132 pidfile_remove(pfh);
133 exit(EXIT_FAILURE);

--- 8 unchanged lines hidden (view full) ---

142 case -1:
143 syslog(LOG_ERR, "Cannot fork(): %s.", strerror(errno));
144 break;
145 case 0:
146 pidfile_close(pfh);
147 /* Do child work. */
148 break;
149 default:
140 syslog(LOG_INFO, "Child %d started.", childpid);
150 syslog(LOG_INFO, "Child %d started.", (int)childpid);
141 break;
142 }
143}
144
145pidfile_remove(pfh);
146exit(EXIT_SUCCESS);
147.Ed
148.Sh ERRORS

--- 91 unchanged lines hidden ---
151 break;
152 }
153}
154
155pidfile_remove(pfh);
156exit(EXIT_SUCCESS);
157.Ed
158.Sh ERRORS

--- 91 unchanged lines hidden ---