Using a PID file is not recommended for daemons serving the root
filesystem, as the required /var/run directories might not be
mounted initially.

And as we're communicating via netlink sockets with the daemon
anyway we can as well add a new command for fcoeadm for printing
out the PID of the daemon.

Signed-off-by: Hannes Reinecke <[email protected]>
---
 etc/initd/initd.suse |   36 +++++++++++++++-------------
 fcoe_clif.h          |    3 ++-
 fcoeadm.c            |   23 +++++++++++++-----
 fcoemon.c            |   65 +++++++++++++-------------------------------------
 4 files changed, 55 insertions(+), 72 deletions(-)

diff --git a/etc/initd/initd.suse b/etc/initd/initd.suse
index e3df550..1d358be 100755
--- a/etc/initd/initd.suse
+++ b/etc/initd/initd.suse
@@ -40,7 +40,6 @@
 ### END INIT INFO
 
 CONFIG_DIR=/etc/fcoe
-PID_FILE="/var/run/fcoemon.pid"
 LOG_FILE="/var/log/fcoemon.log"
 FCOEMON=/usr/sbin/fcoemon
 FCOEADM=/usr/sbin/fcoeadm
@@ -90,7 +89,7 @@ start()
 
        startup_fcoe_modules
 
-       startproc -l ${LOG_FILE} -p ${PID_FILE} ${FCOEMON} ${FCOEMON_OPTS}
+       startproc -l ${LOG_FILE} ${FCOEMON} ${FCOEMON_OPTS}
 
        rc_status -v
 }
@@ -99,7 +98,8 @@ stop()
 {
        local force=$1
 
-       pid=$(pidof "$FCOEMON")
+       echo -n $"Stopping FCoE initiator service: "
+       pid=$($FCOEADM -p)
        if [ "$force" == "force" ]
        then
                echo -n "Destroying any active fcoe interface/s"
@@ -109,9 +109,6 @@ stop()
                [ "$pid" ] && kill -TERM $pid
        fi
 
-       echo -n $"Stopping FCoE initiator service: "
-
-       rm -f ${PID_FILE}
 
        rc_status -v
 }
@@ -119,19 +116,24 @@ stop()
 status()
 {
        echo -n "Checking status for fcoe service "
-       checkproc -p ${PID_FILE} ${FCOEMON}
-       rc_status -v
-       if [ $? -ne 0 ]; then
-               return;
-       fi
-
-       interfaces=`$FCOEADM -i 2>&1 | \
-                   awk '/Symbolic Name:/{print $6}' | \
-                   sort | awk '{printf("%s ", $1)}'`
-       if [ -z "$interfaces" ]; then
-               echo "No interfaces created."
+       pid=$($FCOEADM -p 2> /dev/null)
+       if [ $? -eq 12 ]; then
+           rc_failed 3
+           rc_status -v
+       elif [ -z "$pid" ] ; then
+           rc_failed 1
+           rc_status -v
        else
+           rc_reset
+           rc_status -v
+           interfaces=`$FCOEADM -i 2>&1 | \
+               awk '/Symbolic Name:/{print $6}' | \
+               sort | awk '{printf("%s ", $1)}'`
+           if [ -z "$interfaces" ]; then
+               echo "No interfaces created."
+           else
                echo "Created interfaces: $interfaces"
+           fi
        fi
 }
 
diff --git a/fcoe_clif.h b/fcoe_clif.h
index d67b75f..0c3d099 100644
--- a/fcoe_clif.h
+++ b/fcoe_clif.h
@@ -39,7 +39,8 @@ enum clif_action {
        CLIF_CREATE_CMD,
        CLIF_DESTROY_CMD,
        CLIF_RESET_CMD,
-       CLIF_SCAN_CMD
+       CLIF_SCAN_CMD,
+       CLIF_PID_CMD
 };
 
 /**
diff --git a/fcoeadm.c b/fcoeadm.c
index 6ee17cb..b88a648 100644
--- a/fcoeadm.c
+++ b/fcoeadm.c
@@ -33,7 +33,7 @@
 #include "fcoe_clif.h"
 #include "fcoeadm_display.h"
 
-static const char *optstring = "c:d:r:S:iftlsbhv";
+static const char *optstring = "c:d:r:S:iftlsbhpv";
 static struct option fcoeadm_opts[] = {
        {"create", required_argument, 0, 'c'},
        {"destroy", required_argument, 0, 'd'},
@@ -43,6 +43,7 @@ static struct option fcoeadm_opts[] = {
        {"fcf", no_argument, 0, 'f'},
        {"target", no_argument, 0, 't'},
        {"lun", no_argument, 0, 'l'},
+       {"pid", no_argument, 0, 'p'},
        {"stats", no_argument, 0, 's'},
        {"lesb", no_argument, 0, 'b'},
        {"help", no_argument, 0, 'h'},
@@ -66,6 +67,7 @@ static void fcoeadm_help(void)
               "\t [-l|--lun] [<ethX>]\n"
               "\t [-s|--stats] <ethX> [<interval>]\n"
               "\t [-b|--lesb] <ethX> [<interval>]\n"
+              "\t [-p|--pid]\n"
               "\t [-v|--version]\n"
               "\t [-h|--help]\n\n", progname);
 }
@@ -98,7 +100,7 @@ static enum fcoe_status fcoeadm_clif_request(struct 
clif_sock_info *clif_info,
        }
 }
 
-static enum fcoe_status fcoeadm_request(struct clif_sock_info *clif_info,
+static int fcoeadm_request(struct clif_sock_info *clif_info,
                                        struct clif_data *data)
 {
        char rbuf[MAX_MSGBUF];
@@ -146,7 +148,7 @@ static enum fcoe_status fcoeadm_open_cli(struct 
clif_sock_info *clif_info)
        lp->sun_family = AF_LOCAL;
        lp->sun_path[0] = '\0';
        snprintf(&lp->sun_path[1], sizeof(lp->sun_path) - 1,
-                "%s/%d", CLIF_IFNAME, getpid);
+                "%s/%lu", CLIF_IFNAME, (unsigned long int)getpid);
        addrlen = sizeof(sa_family_t) + strlen(lp->sun_path + 1) + 1;
        if (bind(clif_info->socket_fd, (struct sockaddr *)lp, addrlen) < 0) {
                rc = ENOMONCONN;
@@ -179,14 +181,21 @@ static enum fcoe_status fcoeadm_action(enum clif_action 
cmd, char *ifname)
 {
        struct clif_data data;
        struct clif_sock_info clif_info;
-       enum fcoe_status rc;
+       int rc;
 
-       strncpy(data.ifname, ifname, sizeof(data.ifname));
+       if (ifname)
+               strncpy(data.ifname, ifname, sizeof(data.ifname));
+       else
+               data.ifname[0] = '\0';
        data.cmd = cmd;
 
        rc = fcoeadm_open_cli(&clif_info);
        if (!rc) {
                rc = fcoeadm_request(&clif_info, &data);
+               if (rc > 0 && cmd == CLIF_PID_CMD) {
+                       printf("%d\n", rc);
+                       rc = 0;
+               }
                fcoeadm_close_cli(&clif_info);
        }
 
@@ -244,7 +253,6 @@ int main(int argc, char *argv[])
                        rc = fcoeadm_action(cmd, ifname);
 
                        break;
-
                case 'r':
                        cmd = CLIF_RESET_CMD;
                        /* fall through */
@@ -366,6 +374,9 @@ int main(int argc, char *argv[])
                        if (!rc)
                                rc = display_port_stats(ifname, stat_interval);
                        break;
+               case 'p':
+                       rc = fcoeadm_action(CLIF_PID_CMD, NULL);
+                       break;
 
                case 'b':
                        if (argc > 4) {
diff --git a/fcoemon.c b/fcoemon.c
index 5f60cd9..13af6a9 100644
--- a/fcoemon.c
+++ b/fcoemon.c
@@ -198,8 +198,6 @@ static struct option fcm_options[] = {
 
 char progname[20];
 
-static char fcm_pidfile[] = CLIF_PID_FILE;
-
 /*
  * Issue with buffer size:  It isn't clear how to read more than one
  * buffer's worth of GETLINK replies.  The kernel seems to just drop the
@@ -1787,7 +1785,6 @@ static void fcm_dcbd_shutdown(void)
        FCM_LOG_DBG("Shutdown lldpad connection\n");
        fcm_dcbd_request("D");  /* DETACH_CMD */
        fcm_dcbd_disconnect();
-       unlink(fcm_pidfile);
        closelog();
 }
 
@@ -2901,42 +2898,6 @@ static void fcm_sig(int sig)
        }
 }
 
-static void fcm_pidfile_create(void)
-{
-       FILE *fp;
-       char buf[100];
-       char *sp;
-       int pid;
-       int rc;
-
-       fp = fopen(fcm_pidfile, "r+");
-       if (fp) {
-               if ((sp = fgets(buf, sizeof(buf), fp)) == NULL) {
-                       FCM_LOG("Error reading pid file - exiting\n");
-                       exit(1);
-               }
-               if (!sscanf(sp, "%d", &pid)) {
-                       FCM_LOG("Error reading pid ('%d') - exiting\n",
-                               pid);
-                       exit(1);
-               }
-               rc = kill(pid, 0);
-               if (pid > 0 && !rc) {
-                       FCM_LOG("Another instance"
-                               " (pid %d) is running - exiting\n",
-                               pid);
-                       exit(1);
-               }
-               fclose(fp);
-       }
-       umask(~(S_IRUSR | S_IWUSR));
-       fp = fopen(fcm_pidfile, "w+");
-       if (fp) {
-               fprintf(fp, "%d\n", getpid());
-               fclose(fp);
-       }
-}
-
 /*
  * TODO: This routine does too much. It executes a 'cmd'
  * and allocates a fcoe_port if one doesn't exist. The
@@ -3127,7 +3088,7 @@ static void fcm_srv_receive(void *arg)
        socklen_t fromlen = sizeof(struct sockaddr_un);
        struct sock_info *reply = NULL;
        char buf[MAX_MSGBUF], rbuf[MAX_MSGBUF];
-       char *ifname;
+       char *ifname = NULL;
        enum fcoe_status rc = EFAIL;
        int res, cmd, snum;
 
@@ -3144,12 +3105,13 @@ static void fcm_srv_receive(void *arg)
        data = (struct clif_data *)buf;
 
        cmd = data->cmd;
-       ifname = strdup(data->ifname);
-
-       rc = fcoe_validate_interface(ifname);
-       if (rc)
-               goto err;
+       if (cmd != CLIF_PID_CMD) {
+               ifname = strdup(data->ifname);
 
+               rc = fcoe_validate_interface(ifname);
+               if (rc)
+                       goto err;
+       }
        reply = fcm_alloc_reply(&from, fromlen, snum);
        if (!reply)
                goto err_out;
@@ -3179,17 +3141,25 @@ static void fcm_srv_receive(void *arg)
                if (rc)
                        goto err_out;
                break;
+       case CLIF_PID_CMD:
+               FCM_LOG_DBG("FCMON PID\n");
+               snprintf(rbuf, MAX_MSGBUF, "%lu", (long unsigned int)getpid());
+               sendto(snum, rbuf, strlen(rbuf), 0, (struct sockaddr *)&from,
+                      fromlen);
+               break;
        default:
                FCM_LOG_DBG("Received invalid command %d for %s\n",
                            cmd, ifname);
                goto err_out;
        }
 
-       free(ifname);
+       if (ifname)
+               free(ifname);
        return;
 
 err_out:
-       free(ifname);
+       if (ifname)
+               free(ifname);
        free(reply);
 err:
        snprintf(rbuf, MSG_RBUF, "%d", rc);
@@ -3325,7 +3295,6 @@ int main(int argc, char **argv)
                FCM_LOG_ERR(errno, "Failed to register handler for SIGUSR1");
                exit(1);
        }
-       fcm_pidfile_create();
 
        /* check fcoe module */
        if (fcoe_checkdir(SYSFS_FCOE)) {
-- 
1.7.10.4

_______________________________________________
fcoe-devel mailing list
[email protected]
http://lists.open-fcoe.org/mailman/listinfo/fcoe-devel

Reply via email to