1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
|
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
# Copyright 2013 Nexenta Systems, Inc. All rights reserved.
#
PROG= iscsid
MANIFEST= iscsi-initiator.xml
SVCMETHOD= iscsi-initiator iscsid
include ../Makefile.cmd
CFLAGS += $(CCVERBOSE)
LDLIBS += -lnsl
ROOTMANIFESTDIR= $(ROOTSVCNETWORKISCSI)
$(ROOTSVCNETWORKISCSI)/iscsi-initiator.xml : FILEMODE = 0444
.KEEP_STATE:
all: $(PROG)
check: $(CHKMANIFEST)
clean:
install: all $(ROOTMANIFEST) $(ROOTSVCMETHOD)
include ../Makefile.targ
#!/sbin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
#
#
# Start/stop iscsi initiator service
#
. /lib/svc/share/smf_include.sh
# checkmessage "fsck_device | mount_point"
#
# Simple auxilary routine to the shell function checkfs. Prints out
# instructions for a manual file system check before entering the shell.
#
checkmessage() {
echo "" > /dev/console
if [ "$1" != "" ] ; then
echo "WARNING - Unable to repair one or more \c" \
> /dev/console
echo "of the following filesystem(s):" > /dev/console
echo "\t$1" > /dev/console
else
echo "WARNING - Unable to repair one or more filesystems." \
> /dev/console
fi
echo "Run fsck manually (fsck filesystem...)." > /dev/console
echo "" > /dev/console
}
#
# checkfs raw_device fstype mountpoint
#
# Check the file system specified. The return codes from fsck have the
# following meanings.
# 0 - file system is unmounted and okay
# 32 - file system is unmounted and needs checking (fsck -m only)
# 33 - file system is already mounted
# 34 - cannot stat device
# 36 - uncorrectable errors detected - terminate normally (4.1 code 8)
# 37 - a signal was caught during processing (4.1 exit 12)
# 39 - uncorrectable errors detected - terminate rightaway (4.1 code 8)
# 40 - for root, same as 0 (used by rcS to remount root)
#
checkfs() {
/sbin/fsck -F $2 -m $1 >/dev/null 2>&1
if [ $? -ne 0 ]
then
# Determine fsck options by file system type
case "$2" in
ufs) foptions="-o p"
;;
*) foptions="-y"
;;
esac
echo "The "$3" file system ("$1") is being checked."
/sbin/fsck -F $2 ${foptions} $1
case $? in
0|40) # file system OK
;;
*) # couldn't fix the file system
echo "/sbin/fsck failed with exit code "$?"."
checkmessage "$1"
;;
esac
fi
}
mount_iscsi() {
err=0
iscsilist=""
exec < /etc/vfstab
while read special fsckdev mountp fstype fsckpass automnt mntopts; do
case $special in
'#'* | '' ) # Ignore comments, empty lines.
continue
;;
'-') # Ignore "no-action" lines.
continue
;;
esac
if [ "$automnt" != "iscsi" ]; then
continue
fi
if [ "$fstype" = "-" ]; then
echo "iscsi-initiator: FSType of iscsi LUN \c" 1>&2
echo "$special cannot be identified" 1>&2
continue
fi
#
# Ignore entries already mounted
#
/bin/grep " $mountp " /etc/mnttab >/dev/null \
2>&1 && continue
#
# Can't fsck if no fsckdev is specified
#
if [ "$fsckdev" = "-" ]; then
iscsilist="$iscsilist $mountp"
continue
fi
#
# fsck everything else:
#
# fsck -m simply returns true if the filesystem is
# suitable for mounting.
#
/sbin/fsck -m -F $fstype $fsckdev >/dev/null 2>&1
case $? in
0|40) iscsilist="$iscsilist $mountp"
continue
;;
32) checkfs $fsckdev $fstype $mountp
iscsilist="$iscsilist $mountp"
continue
;;
33) # already mounted
echo "$special already mounted"
;;
34) # bogus special device
echo "Cannot stat $fsckdev - ignoring"
err=1
;;
*) # uncorrectable errors
echo "$fsckdev uncorrectable error"
err=1
;;
esac
done
[ -z "$iscsilist" ] || /sbin/mount -a $iscsilist
for iscsilun in $iscsilist
do
/bin/grep " $iscsilun " /etc/mnttab >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Fail to mount $iscsilun"
err=1
fi
done
return $err
}
umount_iscsi () {
#
# Generate iscsi mountp list from /etc/vfstab
exec < /etc/vfstab
while read special fsckdev mountp fstype fsckpass automnt mntopts; do
case $special in
'#'* | '') continue;; # Ignore comments,
# empty lines.
'-') continue;; # Ignore "no-action lines.
esac
if [ "$automnt" != "iscsi" ]; then
continue
fi
/bin/grep " $mountp " /etc/mnttab >/dev/null 2>&1
if [ $? -ne 0 ]; then
continue
fi
iscsilist="$iscsilist $mountp"
done
if [ -n "$iscsilist" ]; then
umount -a $iscsilist 1>&2
rc=$?
else
rc=0;
fi
return $rc
}
case "$1" in
'start')
/usr/bin/pgrep -P 1 -x iscsid
if [ $? -ne 0 ]; then
/lib/svc/method/iscsid
fi
if [ $? -eq 0 ]; then
delay=60
while [ $delay -gt 0 ]; do
delay=`expr $delay - 1`
mount_iscsi
if [ $? -eq 1 ]; then
if [ $delay -gt 0 ]; then
sleep 1
continue
else
echo "iscsi-initiator: mount iscsi \c"
echo "lun in /etc/vfstab fail."
umount_iscsi
exit $SMF_EXIT_ERR_CONFIG
fi
else
exit $SMF_EXIT_OK
fi
done
else
exit $?
fi
;;
'stop')
umount_iscsi
/usr/bin/pkill -P 1 -x iscsid
exit 0
;;
*)
echo "Usage: $0 { start | stop }"
exit 1
;;
esac
exit $SMF_EXIT_OK
<?xml version='1.0'?>
<!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'>
<!--
CDDL HEADER START
The contents of this file are subject to the terms of the
Common Development and Distribution License (the "License").
You may not use this file except in compliance with the License.
You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
or http://www.opensolaris.org/os/licensing.
See the License for the specific language governing permissions
and limitations under the License.
When distributing Covered Code, include this CDDL HEADER in each
file and include the License file at usr/src/OPENSOLARIS.LICENSE.
If applicable, add the following below this CDDL HEADER, with the
fields enclosed by brackets "[]" replaced with your own identifying
information: Portions Copyright [yyyy] [name of copyright owner]
CDDL HEADER END
Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
Service manifests for the iSCSI Initiator
-->
<!--
GLXXX: Instance creation guidelines:
Currently inetd doesn't support property composition in the configuration of
it's instances. It expects to find the "proto" property in the instances
"inetd_private" property group, and all other properties (including the
default back ones) in the "inetd_private" property group of the service.
This means that only the "proto" field can be specialized for an instance, which
limits the creation of instances of a service to only those with all fields
common, bar the "proto" field. This would enable the following two services
to be created as instances of a common service:
exec stream tcp nowait root /usr/sbin/in.rexecd in.rexecd
exec stream tcp6 nowait root /usr/sbin/in.rexecd in.rexecd
but dissallow common service create for these (different socket type):
time stream tcp6 nowait root internal
time dgram udp6 wait root internal
To be more specific, for rpc services all the netids associated with the
service would need a seperate instance creating under a common service, with
the instance name and the "proto" being the netid. For non-rpc based
services only services with changes limited to the "proto" field (such as
udp/upd6 and tcp/tcp6) can have instances created under a common service - as
in the exec example above.
I neglected to mention that the composition limitation applies for methods
also, so an instance can't have a different method than its service.
Inetd is soon going to change to use instance composition for its configuration.
This will mean that any of the properties/methods can be present in either the
instance or the service (with the instance one overriding in the case both are
present) and that multiple instances can be created for a service with the
potential to specialize all the properties/methods.
-->
<service_bundle type='manifest' name='SUNWiscsir:iscsi-initiator'>
<service
name='network/iscsi/initiator'
type='service'
version='1'>
<single_instance/>
<dependency
name='network'
grouping='require_any'
restart_on='error'
type='service'>
<service_fmri value='svc:/milestone/network' />
</dependency>
<dependency
name='net'
grouping='require_all'
restart_on='none'
type='service'>
<service_fmri value='svc:/network/service' />
</dependency>
<dependency
name='loopback'
grouping='require_any'
restart_on='error'
type='service'>
<service_fmri value='svc:/network/loopback' />
</dependency>
<dependent
name='iscsi-initiator_multi-user'
grouping='optional_all'
restart_on='none'>
<service_fmri value='svc:/milestone/multi-user' />
</dependent>
<!--
Set a timeout of -1 to signify to inetd that we don't want
to timeout this service, since the forked process is the
one that does the services work. This is the case for most/all
legacy inetd services; for services written to take advantage
of Greenlines capabilities, the start method should fork
off a process to handle the request and return a success code.
-->
<exec_method
type='method'
name='start'
exec='/lib/svc/method/iscsi-initiator %m'
timeout_seconds='600'>
<method_context working_directory='/'>
<method_credential
user='root'
group='root'
privileges='basic,sys_devices,sys_mount'
/>
</method_context>
</exec_method>
<exec_method
type='method'
name='stop'
exec='/lib/svc/method/iscsi-initiator %m'
timeout_seconds='600'>
<method_context working_directory='/'>
<method_credential
user='root'
group='root'
privileges='basic,sys_devices,sys_mount'
/>
</method_context>
</exec_method>
<!--
Create an enabled instance.
-->
<instance
name='default'
enabled='true' >
</instance>
<stability value='Evolving' />
<template>
<common_name>
<loctext xml:lang='C'>
iSCSI initiator daemon
</loctext>
</common_name>
<documentation>
<manpage
title='iscsi'
section='4D'
manpath='/usr/share/man' />
</documentation>
</template>
</service>
</service_bundle>
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2012 Milan Jurik. All rights reserved.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <signal.h>
#include <locale.h>
#include <syslog.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <door.h>
#include <libsysevent.h>
#include <wait.h>
#include <semaphore.h>
#include <libscf.h>
#include <sys/scsi/adapters/iscsi_door.h>
#include <sys/scsi/adapters/iscsi_if.h>
/*
* Local Defines
* -------------
*/
#define ISCSI_DOOR_DAEMON_SYSLOG_PP "iscsid"
#define ISCSI_DISCOVERY_POLL_DELAY1 1 /* Seconds */
#define ISCSI_DISCOVERY_POLL_DELAY2 60 /* Seconds */
#define ISCSI_SMF_OFFLINE_DELAY 10 /* Seconds */
#define ISCSI_SMF_OFFLINE_MAX_RETRY_TIMES 60
#if !defined(SMF_EXIT_ERR_OTHER)
#define SMF_EXIT_ERR_OTHER -1
#endif
/*
* Global Variables related to the synchronization of the child process
* --------------------------------------------------------------------
*/
static pid_t iscsi_child_pid;
static sem_t iscsi_child_sem;
static int iscsi_child_door_handle;
static int iscsi_child_smf_exit_code;
/*
* Global Variables related to the door accessed by the kernel
* -----------------------------------------------------------
*/
static int iscsi_dev_handle;
static int iscsi_kernel_door_handle;
/*
* Prototypes of Functions the body of which is defined farther down
* in this file.
* -----------------------------------------------------------------
*/
static void call_child_door(int value);
static void sigchld_handler(int sig);
static boolean_t discovery_event_wait(int did);
static void signone(int, siginfo_t *, void *);
static
void
iscsi_child_door(
void *cookie,
char *args,
size_t alen,
door_desc_t *ddp,
uint_t ndid
);
static
void
iscsi_kernel_door(
void *cookie,
char *args,
size_t alen,
door_desc_t *ddp,
uint_t ndid
);
static
iscsi_door_cnf_t *
_getipnodebyname_req(
getipnodebyname_req_t *req,
int req_len,
size_t *pcnf_len
);
/*
* main -- Entry point of the iSCSI door server daemon
*
* This function forks, waits for the child process feedback and exits.
*/
/* ARGSUSED */
int
main(
int argc,
char *argv[]
)
{
int i;
int sig;
int ret = -1;
int retry = 0;
sigset_t sigs, allsigs;
struct sigaction act;
uint32_t rval;
/*
* Get the locale set up before calling any other routines
* with messages to ouput.
*/
(void) setlocale(LC_ALL, "");
openlog("ISCSI_DOOR_DAEMON_SYSLOG_PP", LOG_PID, LOG_DAEMON);
/* The child semaphore is created. */
if (sem_init(&iscsi_child_sem, 0, 0) == -1) {
exit(SMF_EXIT_ERR_OTHER);
}
/* The door for the child is created. */
iscsi_child_door_handle = door_create(iscsi_child_door, NULL, 0);
if (iscsi_child_door_handle == -1) {
(void) sem_destroy(&iscsi_child_sem);
exit(SMF_EXIT_ERR_OTHER);
}
/* A signal handler is set for SIGCHLD. */
(void) signal(SIGCHLD, sigchld_handler);
/*
* Here begins the daemonizing code
* --------------------------------
*/
iscsi_child_pid = fork();
if (iscsi_child_pid < 0) {
/* The fork failed. */
syslog(LOG_DAEMON | LOG_ERR, gettext("Cannot fork"));
(void) sem_destroy(&iscsi_child_sem);
exit(SMF_EXIT_ERR_OTHER);
}
if (iscsi_child_pid) {
/*
* The parent exits after the child has provided feedback. This
* waiting phase is to meet one of greenline's requirements.
* We shouldn't return till we are sure the service is ready to
* be provided.
*/
(void) sem_wait(&iscsi_child_sem);
(void) sem_destroy(&iscsi_child_sem);
exit(iscsi_child_smf_exit_code);
}
/*
* stdout and stderr are redirected to "/dev/null".
*/
i = open("/dev/null", O_RDWR);
(void) dup2(i, 1);
(void) dup2(i, 2);
/*
* Here ends the daemonizing code
* ------------------------------
*/
/*
* Block out all signals
*/
(void) sigfillset(&allsigs);
(void) pthread_sigmask(SIG_BLOCK, &allsigs, NULL);
/* setup the door handle */
iscsi_kernel_door_handle = door_create(iscsi_kernel_door, NULL, 0);
if (iscsi_kernel_door_handle == -1) {
perror(gettext("door_create failed"));
syslog(LOG_DAEMON | LOG_ERR, gettext("door_create failed"));
exit(SMF_EXIT_ERR_OTHER);
}
/*
* The iSCSI driver is opened.
*/
iscsi_dev_handle = open(ISCSI_DRIVER_DEVCTL, O_RDWR);
if (iscsi_dev_handle == -1) {
/* The driver couldn't be opened. */
perror(gettext("iscsi device open failed"));
exit(SMF_EXIT_ERR_OTHER);
}
if (ioctl(
iscsi_dev_handle,
ISCSI_SMF_ONLINE,
&iscsi_kernel_door_handle) == -1) {
(void) close(iscsi_dev_handle);
perror(gettext("ioctl: enable iscsi initiator"));
exit(SMF_EXIT_ERR_OTHER);
}
/*
* Keep the dev open, so to keep iscsi module from unloaded.
* This is crutial to guarantee the consistency of the
* door_handle and service state in kernel.
*/
/* We have to wait for the discovery process to finish. */
(void) discovery_event_wait(iscsi_dev_handle);
/* We let the parent know that everything is ok. */
call_child_door(SMF_EXIT_OK);
/* now set up signals we care about */
(void) sigemptyset(&sigs);
(void) sigaddset(&sigs, SIGTERM);
(void) sigaddset(&sigs, SIGINT);
(void) sigaddset(&sigs, SIGQUIT);
/* make sure signals to be enqueued */
act.sa_flags = SA_SIGINFO;
act.sa_sigaction = signone;
(void) sigaction(SIGTERM, &act, NULL);
(void) sigaction(SIGINT, &act, NULL);
(void) sigaction(SIGQUIT, &act, NULL);
/* wait and process signals */
for (;;) {
sig = sigwait(&sigs);
if (sig < 0)
continue;
switch (sig) {
case SIGQUIT:
case SIGINT:
case SIGTERM:
do {
ret = ioctl(iscsi_dev_handle,
ISCSI_SMF_OFFLINE, &rval);
if (ret == -1) {
/*
* Keep retrying if unable
* to stop
*/
(void) sleep(ISCSI_SMF_OFFLINE_DELAY);
retry++;
}
} while ((ret == -1) &&
(retry < ISCSI_SMF_OFFLINE_MAX_RETRY_TIMES));
(void) close(iscsi_dev_handle);
if (rval == B_FALSE) {
syslog(LOG_DAEMON, gettext("iSCSI initiator"
" service exited with sessions left."));
}
return (0);
default:
break;
}
}
}
/*
* sigchld_handler -- SIGCHLD Handler
*
*/
/* ARGSUSED */
static
void
sigchld_handler(
int sig
)
{
int status;
pid_t ret_pid;
/* This is the default code. */
iscsi_child_smf_exit_code = SMF_EXIT_ERR_OTHER;
ret_pid = waitpid(iscsi_child_pid, &status, WNOHANG);
if (ret_pid == iscsi_child_pid) {
if (WIFEXITED(status)) {
iscsi_child_smf_exit_code = WEXITSTATUS(status);
}
}
(void) sem_post(&iscsi_child_sem);
}
/*
* iscsi_child_door -- Child process door entry point
*
* This function is executed when a driver calls door_ki_upcall().
*/
/* ARGSUSED */
static
void
iscsi_child_door(
void *cookie,
char *args,
size_t alen,
door_desc_t *ddp,
uint_t ndid
)
{
int *ptr = (int *)args;
iscsi_child_smf_exit_code = SMF_EXIT_ERR_OTHER;
if (alen >= sizeof (iscsi_child_smf_exit_code)) {
iscsi_child_smf_exit_code = *ptr;
}
(void) sem_post(&iscsi_child_sem);
(void) door_return(NULL, 0, NULL, 0);
}
/*
* iscsi_kernel_door -- Kernel door entry point
*
* This function is executed when a driver calls door_ki_upcall().
*/
/* ARGSUSED */
static
void
iscsi_kernel_door(
void *cookie,
char *args,
size_t alen,
door_desc_t *ddp,
uint_t ndid
)
{
iscsi_door_msg_hdr_t err_ind;
iscsi_door_req_t *req;
iscsi_door_cnf_t *cnf;
size_t cnf_len;
char *err_txt;
int err_code;
/* Local variables pre-initialization */
err_ind.signature = ISCSI_DOOR_REQ_SIGNATURE;
err_ind.version = ISCSI_DOOR_REQ_VERSION_1;
err_ind.opcode = ISCSI_DOOR_ERROR_IND;
req = (iscsi_door_req_t *)args;
cnf = (iscsi_door_cnf_t *)&err_ind;
cnf_len = sizeof (err_ind);
/*
* The validity of the request is checked before going any farther.
*/
if (req == NULL) {
/*
* A request has to be passed.
*/
err_ind.status = ISCSI_DOOR_STATUS_REQ_INVALID;
} else if (alen < sizeof (iscsi_door_msg_hdr_t)) {
/*
* The buffer containing the request must be at least as big
* as message header.
*/
err_ind.status = ISCSI_DOOR_STATUS_REQ_LENGTH;
} else if (req->hdr.signature != ISCSI_DOOR_REQ_SIGNATURE) {
/*
* The request must be correctly signed.
*/
err_ind.status = ISCSI_DOOR_STATUS_REQ_INVALID;
} else if (req->hdr.version != ISCSI_DOOR_REQ_VERSION_1) {
/*
* The version of the request must be supported by the server.
*/
err_ind.status = ISCSI_DOOR_STATUS_REQ_VERSION;
} else {
/*
* The request is treated according to the opcode.
*/
switch (req->hdr.opcode) {
case ISCSI_DOOR_GETIPNODEBYNAME_REQ:
cnf = _getipnodebyname_req(
&req->ginbn_req,
alen,
&cnf_len);
break;
default:
err_ind.status = ISCSI_DOOR_STATUS_REQ_INVALID;
break;
}
}
err_code = door_return((char *)cnf, cnf_len, NULL, 0);
switch (err_code) {
case E2BIG:
err_txt = "E2BIG";
break;
case EFAULT:
err_txt = "EFAULT";
break;
case EINVAL:
err_txt = "EINVAL";
break;
case EMFILE:
err_txt = "EMFILE";
break;
default:
err_txt = "?";
break;
}
(void) fprintf(stderr, "door_return error(%s,%d)", err_txt, err_code);
syslog(
LOG_DAEMON | LOG_ERR,
gettext("!door_return error(%s,%d)"),
err_txt,
err_code);
}
/*
* _getipnodebyname_req
*
* This function executes the request ISCSI_DOOR_GETIPNODEBYNAME_REQ. It
* calls getipnodebyname() but doesn't return all the information. The
* confirmation structure only contains one IP address of the list returned
* by getipnodebyname().
*/
static
iscsi_door_cnf_t *
_getipnodebyname_req(
getipnodebyname_req_t *req,
int req_len,
size_t *pcnf_len
) {
getipnodebyname_cnf_t *cnf = (getipnodebyname_cnf_t *)req;
size_t cnf_len;
struct hostent *hptr;
char *name;
/* The opcode is changed immediately. */
cnf->hdr.opcode = ISCSI_DOOR_GETIPNODEBYNAME_CNF;
/* The size of the request is checked against the minimum required. */
if (req_len < sizeof (getipnodebyname_cnf_t)) {
cnf->hdr.status = ISCSI_DOOR_STATUS_REQ_FORMAT;
*pcnf_len = req_len;
return ((iscsi_door_cnf_t *)cnf);
}
name = (char *)req + req->name_offset;
/*
* The pointer to the name has to stay inside the request but
* after the header.
*/
if ((name < ((char *)req + sizeof (getipnodebyname_req_t))) ||
((name + req->name_length) > ((char *)req + req_len))) {
cnf->hdr.status = ISCSI_DOOR_STATUS_REQ_FORMAT;
*pcnf_len = req_len;
return ((iscsi_door_cnf_t *)cnf);
}
/* The library function is called. */
hptr = getipnodebyname(
name,
(int)req->af,
(int)req->flags,
(int *)&cnf->error_num);
if (hptr) {
/*
* The call was successful. Now starts the painful work of
* parsing the data. However, for version 1 we will only
* return the first address.
*/
cnf_len = sizeof (getipnodebyname_cnf_t);
cnf->h_size_needed = sizeof (getipnodebyname_cnf_t);
cnf->h_alias_list_length = 0;
cnf->h_alias_list_offset = 0;
cnf->h_name_len = 0;
cnf->h_name_offset = 0;
cnf->h_addrlen = (uint32_t)hptr->h_length;
cnf->h_addrtype = (uint32_t)hptr->h_addrtype;
cnf->h_addr_list_offset = sizeof (getipnodebyname_cnf_t);
if (*hptr->h_addr_list != NULL) {
(void) memcpy(
((char *)cnf + sizeof (getipnodebyname_cnf_t)),
*hptr->h_addr_list,
hptr->h_length);
cnf->h_addr_list_length = 1;
cnf->h_size_needed += cnf->h_addrlen;
cnf_len += hptr->h_length;
} else {
cnf->h_addr_list_length = 0;
cnf->h_size_needed += hptr->h_length;
}
*pcnf_len = cnf_len;
cnf->hdr.status = ISCSI_DOOR_STATUS_SUCCESS;
freehostent(hptr);
} else {
cnf->hdr.status = ISCSI_DOOR_STATUS_SUCCESS;
cnf->h_addrlen = 0;
cnf->h_addrtype = 0;
cnf->h_addr_list_offset = sizeof (getipnodebyname_cnf_t);
cnf->h_addr_list_length = 0;
cnf->h_name_offset = sizeof (getipnodebyname_cnf_t);
cnf->h_name_len = 0;
cnf->h_alias_list_offset = sizeof (getipnodebyname_cnf_t);
cnf->h_alias_list_length = 0;
cnf->h_size_needed = sizeof (getipnodebyname_cnf_t);
*pcnf_len = sizeof (getipnodebyname_cnf_t);
}
return ((iscsi_door_cnf_t *)cnf);
}
/*
* call_child_door -- This function calls the child door with the value
* provided by the caller.
*
*/
static
void
call_child_door(
int value
)
{
door_arg_t door_arg;
(void) memset(&door_arg, 0, sizeof (door_arg));
door_arg.data_ptr = (char *)&value;
door_arg.data_size = sizeof (value);
(void) door_call(iscsi_child_door_handle, &door_arg);
}
/*
* get_luns_count --
*/
static
uint32_t
get_luns_count(
int did
)
{
iscsi_lun_list_t *lun_list;
iscsi_lun_list_t *tmp;
size_t len;
uint32_t lun_count;
lun_list = (iscsi_lun_list_t *)malloc(sizeof (*lun_list));
(void) memset(lun_list, 0, sizeof (*lun_list));
lun_list->ll_vers = ISCSI_INTERFACE_VERSION;
lun_list->ll_in_cnt = 1;
lun_list->ll_all_tgts = B_TRUE;
for (;;) {
if (ioctl(
did,
ISCSI_LUN_OID_LIST_GET,
lun_list) == -1) {
free(lun_list);
/* The Ioctl didn't go well. */
return (0);
}
if (lun_list->ll_in_cnt >= lun_list->ll_out_cnt) {
/* We got it all. */
break;
}
/*
* We didn't get all the targets. Let's build a new Ioctl with
* a new size.
*/
tmp = lun_list;
len = tmp->ll_out_cnt * sizeof (tmp->ll_luns);
len += sizeof (*tmp) - sizeof (tmp->ll_luns);
lun_list = (iscsi_lun_list_t *)malloc(len);
if (lun_list == NULL) {
/* No resources. */
free(tmp);
return (0);
}
(void) memset(lun_list, 0, len);
lun_list->ll_vers = ISCSI_INTERFACE_VERSION;
lun_list->ll_in_cnt = tmp->ll_out_cnt;
lun_list->ll_all_tgts = B_TRUE;
free(tmp);
}
lun_count = lun_list->ll_out_cnt;
free(lun_list);
return (lun_count);
}
/*
* discovery_event_wait -- Waits for the discovery process to finish.
*
*/
static
boolean_t
discovery_event_wait(
int did
)
{
boolean_t rc;
uint32_t lun_count;
uint32_t lun_timer;
uint32_t tmp;
iSCSIDiscoveryMethod_t discovery_flags;
iSCSIDiscoveryMethod_t discovery_all;
rc = B_FALSE;
lun_count = 0;
lun_timer = 0;
discovery_flags = 0;
discovery_all = iSCSIDiscoveryMethodStatic |
iSCSIDiscoveryMethodSLP |
iSCSIDiscoveryMethodISNS |
iSCSIDiscoveryMethodSendTargets;
for (;;) {
/* The status discovery flags are read. */
if (ioctl(
did,
ISCSI_DISCOVERY_EVENTS,
&discovery_flags) == -1) {
/* IO problem */
break;
}
if (discovery_flags == discovery_all) {
/* Discovery over */
rc = B_TRUE;
break;
}
if (lun_timer >= ISCSI_DISCOVERY_POLL_DELAY2) {
/* Let's check if the driver is making progress. */
tmp = get_luns_count(did);
if (tmp <= lun_count) {
/* No progress */
break;
}
lun_count = tmp;
lun_timer = 0;
}
(void) sleep(ISCSI_DISCOVERY_POLL_DELAY1);
lun_timer += ISCSI_DISCOVERY_POLL_DELAY1;
}
return (rc);
}
/*ARGSUSED*/
static void
signone(int sig, siginfo_t *sip, void *utp)
{
}
|