PERFORCE change 82503 for review

soc-tyler soc-tyler at FreeBSD.org
Wed Aug 24 17:56:29 GMT 2005


http://perforce.freebsd.org/chv.cgi?CH=82503

Change 82503 by soc-tyler at soc-tyler_launchd on 2005/08/24 17:56:11

	Remove a lot of the stupid fprintf() debugging statements now that
	things "sort of just work(tm)" 
	
	Add in pseudo-fix for ADC bug # 4229893 (liblaunch's auto-start being
	completely brain-dead ;)) by adding some error checking. 
	
	Also remove that BSDL nonsense, since the APSL trumps it, and makes 
	me a saaaaad panda. (wtf was I thinking O_o)
	
	Beginning work on loading jobs into launchd via launchctl(1)

Affected files ...

.. //depot/projects/soc2005/launchd/NOTES#6 edit
.. //depot/projects/soc2005/launchd/includes/launch.h#11 edit
.. //depot/projects/soc2005/launchd/includes/launchd.h#8 edit
.. //depot/projects/soc2005/launchd/launchd.c#14 edit
.. //depot/projects/soc2005/launchd/liblaunch.c#12 edit

Differences ...

==== //depot/projects/soc2005/launchd/NOTES#6 (text+ko) ====

@@ -9,14 +9,6 @@
 	There are no plans to incorporate this aspect of launchd(8) into FreeBSD until 
 	Zeroconf is stable, and in the base system of FreeBSD (6.xx, 7.xx, X.xx ;))
 	
-
-	o launch_msg() doesn't need to be reimplemented until launchctl(1) is revamped
-	to work fully on FreeBSD (well, except for the controlling of launchd(8) part)
-	
-	After launchctl(1) is modified correctly, removal of CoreFoundation and 
-	Zeroconf calls from launchd(8) can be made, and finally liblaunch can be 
-	fixed to accomodate.
-	
 	
 	o The stock source comes with a modified init.c from Mac OS 10.3 and 
 	previously which was derived from the standard BSD init.c some eternity ago.

==== //depot/projects/soc2005/launchd/includes/launch.h#11 (text+ko) ====

@@ -3,26 +3,6 @@
  *
  * Copyright (c) 2005 R. Tyler Ballance <tyler at tamu.edu> All rights reserved.
  *
- *	Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 
- * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE * 
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
  */
 
 /*

==== //depot/projects/soc2005/launchd/includes/launchd.h#8 (text+ko) ====

@@ -3,26 +3,6 @@
  *
  * Copyright (c) 2005 R. Tyler Ballance <tyler at tamu.edu> All rights reserved.
  *
- *	Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 
- * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE * 
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
  */
 
 /*

==== //depot/projects/soc2005/launchd/launchd.c#14 (text+ko) ====

@@ -3,26 +3,6 @@
  *
  * Copyright (c) 2005 R. Tyler Ballance <tyler at tamu.edu> All rights reserved.
  *
- *	Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 
- * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE * 
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
  */
 
 /*

==== //depot/projects/soc2005/launchd/liblaunch.c#12 (text+ko) ====

@@ -3,26 +3,6 @@
  *
  * Copyright (c) 2005 R. Tyler Ballance <tyler at tamu.edu> All rights reserved.
  *
- *	Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 
- * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE * 
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
  */
 
 /*
@@ -53,6 +33,7 @@
 #include <sys/fcntl.h>
 #include <sys/un.h>
 #include <sys/uio.h>
+#include <sys/wait.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -122,21 +103,13 @@
 	char *where = getenv(LAUNCHD_SOCKET_ENV);
 	char *_launchd_fd = getenv(LAUNCHD_TRUSTED_FD_ENV);
 	int r, dfd, lfd = -1, tries;
+	pid_t _launchd_pid;
 
-#ifdef _LAUNCHD_
-	fprintf(stderr,"starting launch_client_init() in liblaunch.c:%d\n", __LINE__);
-	fprintf(stderr,"where = \"%s\"\n", where);
-	fprintf(stderr,"_launchd_fd = \"%s\"\n", _launchd_fd);
-#endif
-	
 	_lc = calloc(1, sizeof(struct _launch_client));
 
 	// if we can't calloc() _lc, exit function
 	if (_lc == NULL) 
-		return;
-#ifdef _LAUNCHD_
-	fprintf(stderr,"calling pthread_mutex_init() in liblaunch.c:%d\n", __LINE__);
-#endif
+	
 	pthread_mutex_init(&_lc->mtx, NULL);
 
 	if (_launchd_fd != NULL) {
@@ -150,11 +123,6 @@
 		unsetenv(LAUNCHD_TRUSTED_FD_ENV);
 	}
 
-#ifdef _LAUNCHD_
-	fprintf(stderr,"Reached liblaunch.c:%d and continuing with execution from here\n", 
-				__LINE__);
-#endif	
-
 	if (lfd == -1) {
 		memset(&sun, 0, sizeof(sun));
 		sun.sun_family = AF_UNIX;
@@ -164,71 +132,57 @@
 		else
 			snprintf(sun.sun_path, sizeof(sun.sun_path), "%s/%u/sock", LAUNCHD_SOCK_PREFIX, getuid());
 
-#ifdef _LAUNCHD_
-		fprintf(stderr,"Successfully gotten the socket's path in liblaunch.c:%d\n", 
-				__LINE__);
-#endif
-
-		if ((lfd = _fd(socket(AF_UNIX, SOCK_STREAM, 0))) == -1) {
-#ifdef _LAUNCHD_
-			fprintf(stderr,"_fd() call didn't succeed in liblaunch.c:%d\n", 
-				__LINE__);
-#endif
+		if ((lfd = _fd(socket(AF_UNIX, SOCK_STREAM, 0))) == -1) 
 			goto out_bad;
-		}
 
 		for (tries = 0; tries < 10; tries++) {
 			r = connect(lfd, (struct sockaddr *)&sun, sizeof(sun));
 			if (r == -1) {
-				if (fork() == 0)
-					execl(LAUNCHD_PATH, LAUNCHD_PATH, NULL);
+				_launchd_pid = fork();
+				
+				if (_launchd_pid == 0) {
+					// start launchd(8) as a daemon
+					if (execl(LAUNCHD_PATH, LAUNCHD_PATH, "-d", NULL) != 0) { 
+						fprintf(stderr,"execl() returned %s in %s:%d",
+							 strerror(errno), __FILE__, __LINE__);
+						exit(EXIT_FAILURE);
+					}
+				} 
+		
+				else if (_launchd_pid > 0) 
+					waitpid(_launchd_pid, NULL, 0);
+			
+				/* in the case of fork() returning -1 
+				 * we are simply going to loop through and try again 
+				 */
+					
 				sleep(1);
-			} else {
-				break;
-			}
-		}
+			} else 
+				break; // socket connection succeeded, continue.
+		} // end socket connect loop
 
 		if (r == -1) {
 			close(lfd);
-#ifdef _LAUNCHD_
-			fprintf(stderr,"r was equal to -1 in liblaunch.c:%d\n", __LINE__);
-#endif
 			goto out_bad;
 		}
 	}
 
-	if (!(_lc->l = launchd_fdopen(lfd))) {
+	if ((_lc->l = launchd_fdopen(lfd)) == NULL) {
 		close(lfd);
-#ifdef _LAUNCHD_
-		fprintf(stderr,"launchd_fdopen() failed in liblaunch.c:%d\n", __LINE__);
-#endif
 		goto out_bad;
 	}
 
-	if (!(_lc->async_resp = launch_data_alloc(LAUNCH_DATA_ARRAY))) {
-#ifdef _LAUNCHD_
-		fprintf(stderr,"launch_data_alloc() failed in liblaunch.c:%d\n", __LINE__);
-#endif
+	if ((_lc->async_resp = launch_data_alloc(LAUNCH_DATA_ARRAY)) == NULL) 
 		goto out_bad;
-	}
 
 	return;
 
 out_bad:
-#ifdef _LAUNCHD_
-	fprintf(stderr,"Executed a \"goto out_bad\", running out_bad in liblaunch.c:%d\n", 
-			__LINE__);
-#endif
 	if (_lc->l != NULL)
 		launchd_close(_lc->l);
 	if (_lc != NULL)
 		free(_lc);
 	_lc = NULL;
-#ifdef _LAUNCHD_
-	fprintf(stderr,
-		"Finished executing \'out_bad\', setting _lc to NULL in liblaunch.c:%d\n", 
-			__LINE__);
-#endif
 }
 
 launch_data_t launch_data_alloc(launch_data_type_t t) {
@@ -277,11 +231,7 @@
 	free(d);
 }
 
-size_t launch_data_dict_get_count(launch_data_t dict)
-{
-	return dict->_array_cnt / 2;
-}
-
+size_t launch_data_dict_get_count(launch_data_t dict) { return dict->_array_cnt / 2; }
 
 bool launch_data_dict_insert(launch_data_t dict, launch_data_t what, const char *key)
 {
@@ -742,25 +692,13 @@
 {
 	launch_data_t resp = NULL;
 
-#ifdef _LAUNCHD_
-	fprintf(stderr,"Starting launch_msg() in liblaunch.c:%d\n", __LINE__);
-#endif
-
-	// XXX: this call doesn't put something at &_lc
 	pthread_once(&_lc_once, launch_client_init);
 	
-	// XXX: we keep evaluating for true here, and error out
 	if (_lc == NULL) {
-#ifdef _LAUNCHD_
-		fprintf(stderr,"_lc sucks, returning liblaunch.c:%d\n", __LINE__);
-#endif	
 		errno = ENOTCONN;
 		return NULL;
 	}
 
-#ifdef _LAUNCHD_
-	fprintf(stderr,"Executing pthread_mutex_lock() in liblaunch.c:%d\n", __LINE__);
-#endif
 	pthread_mutex_lock(&_lc->mtx);
 
 	if (d && launchd_msg_send(_lc->l, d) == -1) {


More information about the p4-projects mailing list